【发布时间】:2020-03-01 22:09:45
【问题描述】:
我目前正在尝试将几年前创建的 MVVM 库从 .NET 4.5 迁移到 .NET Core 3.1。 这出乎意料地好,但目前我正在为我在 RelayCommand-Class 中使用的 CommandManager-Class 苦苦挣扎。
我正在为我的 RelayCommand-Class 的 CanExecute 事件处理程序使用 CommandManager:
public class RelayCommand : ICommand
{
#region Properties
readonly Action<object> _execute;
readonly Predicate<object> _canExecute;
#endregion
#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
#region ICommand Members
[DebuggerStepThrough]
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
}
在我研究这个问题的过程中,我发现 System.Windows.Input 不是 .NET Core 的一部分。有许多解决方案建议将 Projecttarget 从 Classlibrary 切换到 WPF-Application 或嵌入 PresentationCore-Assembly。
这些解决方案对我不起作用——我猜主要是因为我使用了普通的 .NET Core 类库项目。
所以我想问一下它们是否存在于 .NET Core 中的类似类? 或者如果我尝试编写自己的 CommandManager-Class 来替换它会更好吗?
目前最后一个选项是从我的库中提取 Commanding-part 并将其直接放入使用该库的项目中(一个 avalonia 客户端应用程序)。 但这感觉不对……
亲切的问候
地理编码器
【问题讨论】:
标签: c# .net .net-core class-library