C# 名称为;命令管理器“;在当前上下文中不存在(Visual Studio 2015)
尝试使用下面的RelayCommand类时,我收到错误消息:“名称“CommandManager”在当前上下文中不存在”。根据这篇文章,我试图通过在“AddReference”上添加一个名为PresentationCore的程序集来解决这个问题。不幸的是,“在计算机上找不到框架程序集”。 我卸载了Microsoft.NETCore.UniversalWindowsPlatform,并按照某些帖子中的建议重新安装了它,我修复了Visual Studio,但这并没有改变任何事情。这是我的失败还是一个错误?作为一个新手,我总是觉得我忽略了什么;-)C# 名称为;命令管理器“;在当前上下文中不存在(Visual Studio 2015),c#,visual-studio-2015,relaycommand,C#,Visual Studio 2015,Relaycommand,尝试使用下面的RelayCommand类时,我收到错误消息:“名称“CommandManager”在当前上下文中不存在”。根据这篇文章,我试图通过在“AddReference”上添加一个名为PresentationCore的程序集来解决这个问题。不幸的是,“在计算机上找不到框架程序集”。 我卸载了Microsoft.NETCore.UniversalWindowsPlatform,并按照某些帖子中的建议重新安装了它,我修复了Visual Studio,但这并没有改变任何事情。这是我的失败还是一个
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows.Input;
命名空间MyApplication.Viewmodels
{
公共类中继命令:ICommand
{
私有只读操作\u执行;
私有只读功能可执行;
公共中继命令(操作执行)
:此(执行,空)
{
}
公共RelayCommand(操作执行,函数执行)
{
if(execute==null)
{
抛出新ArgumentNullException(“执行”,“执行不能为null”);
}
_执行=执行;
_CanExecute=CanExecute;
}
公共事件事件处理程序CanExecuteChanged
{
添加{CommandManager.RequerySuggested+=value;}
删除{CommandManager.RequerySuggested-=value;}
}
public void Execute(对象参数)
{
_执行(参数);
}
公共布尔CanExecute(对象参数)
{
如果(_CanExecute==null)
{
返回true;
}
返回_CanExecute(参数);
}
}
}
您正在混合使用标准的.NET Framework
WPF(CommandManager
)和用于UWP开发的较小的.NET核心框架
不幸的是,它们并非在所有领域都100%兼容。您不能使用RelayCommand
类,因为它是在那里编写的-您需要重写依赖CommandManager
的部分,当您创建一个包含基本(继承)类的类之后尝试打开实例时:
public class RelayCommand : ICommand
{
public RelayCommand()
{}
private readonly Action<object> _Execute;
private readonly Func<object, bool> _CanExecute;
public RelayCommand(Action<object> execute)
: this(execute, null)
{
}
公共类RelayCommand:ICommand
{
公共关系司令部()
{}
私有只读操作\u执行;
私有只读功能可执行;
公共中继命令(操作执行)
:此(执行,空)
{
}
在.NETCore中不存在CommandManager。我编写了自己的解释,因为我当时的处境与您现在的处境完全相同。(使用MVVM设计模式开发UWP应用程序)
RelayCommand.cs:
using System;
using System.Windows.Input;
namespace MVVMBoostUniversalWindowsApp
{
public class RelayCommand : ICommand
{
private readonly Action execute;
private readonly Func<bool> canExecute;
public RelayCommand(Action execute)
: this(execute, null)
{ }
public RelayCommand(Action execute, Func<bool> canExecute)
{
if (execute == null)
throw new ArgumentNullException("execute is null.");
this.execute = execute;
this.canExecute = canExecute;
this.RaiseCanExecuteChangedAction = RaiseCanExecuteChanged;
SimpleCommandManager.AddRaiseCanExecuteChangedAction(ref RaiseCanExecuteChangedAction);
}
~RelayCommand()
{
RemoveCommand();
}
public void RemoveCommand()
{
SimpleCommandManager.RemoveRaiseCanExecuteChangedAction(RaiseCanExecuteChangedAction);
}
bool ICommand.CanExecute(object parameter)
{
return CanExecute;
}
public void Execute(object parameter)
{
execute();
SimpleCommandManager.RefreshCommandStates();
}
public bool CanExecute
{
get { return canExecute == null || canExecute(); }
}
public void RaiseCanExecuteChanged()
{
var handler = CanExecuteChanged;
if (handler != null)
{
handler(this, new EventArgs());
}
}
private readonly Action RaiseCanExecuteChangedAction;
public event EventHandler CanExecuteChanged;
}
}
使用系统;
使用System.Windows.Input;
名称空间MVVMBoostInversalWindowsApp
{
公共类中继命令:ICommand
{
私有只读操作执行;
私有只读功能可执行;
公共中继命令(操作执行)
:此(执行,空)
{ }
公共RelayCommand(操作执行,函数执行)
{
if(execute==null)
抛出新ArgumentNullException(“execute为null”);
this.execute=execute;
this.canExecute=canExecute;
this.RaiseCanExecuteChangedAction=RaiseCanExecuteChanged;
SimpleCommand Manager.AddRaiseCanecuteChangedAction(参考RaiseCanecuteChangedAction);
}
~RelayCommand()
{
RemoveCommand();
}
公共无效删除命令()
{
SimpleCommand和Manager.RemoveAISECAnexecutechangedAction(RaiseCanExecuteChangedAction);
}
bool ICommand.CanExecute(对象参数)
{
返回CanExecute;
}
public void Execute(对象参数)
{
执行();
SimpleCommandManager.RefreshCommandState();
}
公共图书馆
{
获取{return canExecute==null | | canExecute();}
}
public void raisecancecutechanged()
{
var handler=CanExecuteChanged;
if(处理程序!=null)
{
处理程序(这是新的EventArgs());
}
}
私有只读操作RaiseCanceTechChangedAction;
公共事件处理程序CanExecuteChanged;
}
}
下面是我的SimpleCommand Manager.cs:
using System;
using System.Collections.Generic;
using System.ComponentModel;
namespace MVVMBoostUniversalWindowsApp
{
public static class SimpleCommandManager
{
private static List<Action> _raiseCanExecuteChangedActions = new List<Action>();
public static void AddRaiseCanExecuteChangedAction(ref Action raiseCanExecuteChangedAction)
{
_raiseCanExecuteChangedActions.Add(raiseCanExecuteChangedAction);
}
public static void RemoveRaiseCanExecuteChangedAction(Action raiseCanExecuteChangedAction)
{
_raiseCanExecuteChangedActions.Remove(raiseCanExecuteChangedAction);
}
public static void AssignOnPropertyChanged(ref PropertyChangedEventHandler propertyEventHandler)
{
propertyEventHandler += OnPropertyChanged;
}
private static void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
{
// this if clause is to prevent an infinity loop
if (e.PropertyName != "CanExecute")
{
RefreshCommandStates();
}
}
public static void RefreshCommandStates()
{
for (var i = 0; i < _raiseCanExecuteChangedActions.Count; i++)
{
var raiseCanExecuteChangedAction = _raiseCanExecuteChangedActions[i];
if (raiseCanExecuteChangedAction != null)
{
raiseCanExecuteChangedAction.Invoke();
}
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
名称空间MVVMBoostInversalWindowsApp
{
公共静态类SimpleCommand管理器
{
私有静态列表_raisecancecutechangedactions=new List();
public static void AddRaiseCanExecuteChangedAction(参考操作raiseCanExecuteChangedAction)
{
_添加(raiseCanExecuteChangedAction);
}
公共静态无效清除器AISECAnexecutechangedAction(Action raiseCanExecuteChangedAction)
{
_Remove(ReaseCanexecuteChangedAction);
}
公共静态无效分配PropertyChanged(参考PropertyChangedEventHandler propertyEventHandler)
{
propertyEventHandler+=OnPropertyChanged;
}
私有静态无效OnPropertyChanged(对象发送方,PropertyChangedEventArgs e)
{
//此if子句用于防止无限循环
如果(如PropertyName!=“CanExecute”)
{
刷新命令状态();
}
}
公共静态void RefreshCommandStates()
{
对于(变量i=0;i<\u raisecancecutechangedactions.Count;i++)
{
var RAISECANEXECUTECHTANGEDACTION=_RAISECANEXECUTECHTANGEDACTIONS[i];
如果(RAISECANEXECUTECHANGEDATION!=null)
{
raiseCannexecutechangedAction.Invoke();
}
}
}
}
}
不要问我的朋友
using System;
using System.Collections.Generic;
using System.ComponentModel;
namespace MVVMBoostUniversalWindowsApp
{
public static class SimpleCommandManager
{
private static List<Action> _raiseCanExecuteChangedActions = new List<Action>();
public static void AddRaiseCanExecuteChangedAction(ref Action raiseCanExecuteChangedAction)
{
_raiseCanExecuteChangedActions.Add(raiseCanExecuteChangedAction);
}
public static void RemoveRaiseCanExecuteChangedAction(Action raiseCanExecuteChangedAction)
{
_raiseCanExecuteChangedActions.Remove(raiseCanExecuteChangedAction);
}
public static void AssignOnPropertyChanged(ref PropertyChangedEventHandler propertyEventHandler)
{
propertyEventHandler += OnPropertyChanged;
}
private static void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
{
// this if clause is to prevent an infinity loop
if (e.PropertyName != "CanExecute")
{
RefreshCommandStates();
}
}
public static void RefreshCommandStates()
{
for (var i = 0; i < _raiseCanExecuteChangedActions.Count; i++)
{
var raiseCanExecuteChangedAction = _raiseCanExecuteChangedActions[i];
if (raiseCanExecuteChangedAction != null)
{
raiseCanExecuteChangedAction.Invoke();
}
}
}
}
}
using System.ComponentModel;
using System;
using System.Runtime.CompilerServices;
using Windows.UI.Xaml.Controls;
using System.Collections.Generic;
using Windows.UI.Core;
namespace MVVMBoostUniversalWindowsApp
{
public abstract class ViewModel : INotifyPropertyChanged
{
protected ViewModel()
{
DispatcherObject = CoreWindow.GetForCurrentThread().Dispatcher;
SimpleCommandManager.AssignOnPropertyChanged(ref this.PropertyChanged);
_commandsList = new List<RelayCommand>();
}
private List<RelayCommand> _commandsList;
protected RelayCommand CreateCommand(Action execute)
{
return CreateCommand(execute, null);
}
protected RelayCommand CreateCommand(Action execute, Func<bool> canExecute)
{
var tempCmd = new RelayCommand(execute, canExecute);
if (_commandsList.Contains(tempCmd))
{
return _commandsList[_commandsList.IndexOf(tempCmd)];
}
else
{
_commandsList.Add(tempCmd);
return tempCmd;
}
}
public void RemoveCommands()
{
for (var i = 0; i < _commandsList.Count; i++)
{
_commandsList[i].RemoveCommand();
}
}
public virtual CoreDispatcher DispatcherObject { get; protected set; }
public event PropertyChangedEventHandler PropertyChanged;
protected void ChangeProperty<T>(ref T property, T value, [CallerMemberName] string propertyName = null)
{
if (Object.Equals(property, value))
{
return;
}
else
{
property = value;
OnPropertyChanged(propertyName);
}
}
protected void OnPropertyChangedEmpty()
{
OnPropertyChanged(String.Empty);
}
protected void OnPropertyChanged(string propertyName)
{
var handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
public RelayCommand(Action<object> execute)
: this(execute, null)
{
}
public class RelayCommand : ICommand
{
public RelayCommand()
{ }