类库无法识别 CommandManager 类

Class library does not recognize CommandManager class(类库无法识别 CommandManager 类)
本文介绍了类库无法识别 CommandManager 类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发 WPF 应用程序,我想重用所有这些应用程序中相同的类,以便将它们添加为参考.

I'm developing WPF applications and I want to reuse my classes that are the same in all those applications so I can add them as a reference.

就我而言,我有一个命令类:

In my case I have a class for my Commands:

public class RelayCommand : ICommand
{
    #region Fields

    readonly Action<object> _execute;
    readonly Predicate<object> _canExecute;

    #endregion // Fields

    #region Constructors

    public RelayCommand(Action<object> execute)
        : this(execute, null)
    {

    }

    public RelayCommand(Action<object> execute, Predicate<object> canExecute)
    {
        if (execute == null)
            throw new ArgumentNullException("execute");

        _execute = execute;
        _canExecute = canExecute;
    }
    #endregion // Constructors

    #region ICommand Members

    public bool CanExecute(object parameter)
    {
        return _canExecute == null ? true : _canExecute(parameter);
    }

    public event EventHandler CanExecuteChanged
    {
        add { CommandManager.RequerySuggested += value; }
        remove { CommandManager.RequerySuggested -= value; }
    }

    public void Execute(object parameter)
    {
        _execute(parameter);
    }

    #endregion // ICommand Members
}

这在我的应用程序中完美运行,但是当我想创建一个只想在项目中添加作为参考的类库时,Visual Studio 无法构建,因为CommandManager 没有存在于当前语境中".在我的使用中,我有以下(应该足够了)

This works perfect in my application, but when I want to make a class library which I just want to add as a reference in my project, visual studio can't build because "CommandManager does not exists in the current context". In my usings I have the following (which should be enough)

 using System;
 using System.Windows.Input;

任何想法为什么我不能在类库项目"中这样做?

Any ideas why I can't do this in a "class library project" ?

推荐答案

转到类库的引用"部分并选择添加引用".查找名为PresentationCore"的程序集并添加它.

Go to the "References" part of your class library and select "Add Reference". Look for an assembly called "PresentationCore" and add it.

然后在你的类文件中添加 using 语句 using System.Windows.Input;

Then in your class file add the using statement using System.Windows.Input;

然后您就可以按预期访问 CommandManager.

You will then be able to access the CommandManager as you expect.

只是补充:很多人在创建类库时,他们选择WPF 自定义控件库",然后删除Class1.cs"文件.这是一个快捷方式,可以自动将正确的命名空间添加到您的库中.捷径是好是坏是任何人的决定,但我一直都在使用它.

Just adding: lots of guys when they go to create a class library, they select "WPF Custom Control Library" and then erase the "Class1.cs" file. It's a shortcut that automatically adds the right namespaces to your library. Whether it's a good or bad shortcut is anybody's call, but I use it all the time.

这篇关于类库无法识别 CommandManager 类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)