类库不承认的CommandManager类 [英] Class library does not recognize CommandManager class
问题描述
我开发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不能因为建设一参命令管理不存在于当前上下文。在我usings我有使用系统(这应该是足够了)
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语句使用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类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!