【问题标题】:Class library does not recognize CommandManager class类库无法识别 CommandManager 类
【发布时间】:2013-06-01 19:05:54
【问题描述】:

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

在我的例子中,我有一个命令类:

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 没有存在于当前语境中”。在我的使用中,我有以下(应该足够了)

 using System;
 using System.Windows.Input;

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

【问题讨论】:

    标签: c# wpf mvvm command class-library


    【解决方案1】:

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

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-23
      • 2014-07-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多