- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有以下问题:
我有一个带有 execute 和 canexecute 方法的中继命令,但每次我调用 raisecanexecutechanged();它在 relaycommand 中调用 raisecanexecutechanged,为其设置一个新委托(delegate),然后返回到 View 模型。
相同的设置适用于另一个 View 模型。我检查了大约 1000 次有什么不同,但我没有找到任何东西。
如果你能帮助我,我将不胜感激。
public RelayCommand UpdateAMSCommand { get; private set; }
public AMSSettingsViewModel(IEventAggregator eventAggregator)
{
UpdateAMSCommand = new RelayCommand(OnUpdateAMS, CanUpdateAms);
CustomAMSOffices.ListChanged += listChanged;
CustomAMSContacts.ListChanged += listChanged;
}
private void listChanged(object sender, ListChangedEventArgs e)
{
if (sender != null)
{
if (sender is BindingList<CustomAMSOffice>)
{
BindingList<CustomAMSOffice> temp = (BindingList<CustomAMSOffice>)sender;
if (temp.Count > _amsOfficesItemsCounter)
{
_amsOfficesItemsCounter = temp.Count;
for (int i = 0; i < temp.Count; i++)
{
temp[i].ErrorsChanged += RaiseCanExecuteChanged;
}
}
}
else if (sender is BindingList<CustomAMSContact>)
{
BindingList<CustomAMSContact> temp = (BindingList<CustomAMSContact>)sender;
if (temp.Count > _amsContactsItemsCounter)
{
_amsContactsItemsCounter = temp.Count;
for (int i = 0; i < temp.Count; i++)
{
temp[i].ErrorsChanged += RaiseCanExecuteChanged;
}
}
}
}
UpdateAMSCommand.RaiseCanExecuteChanged();
}
private void RaiseCanExecuteChanged(object sender, DataErrorsChangedEventArgs e)
{
UpdateAMSCommand.RaiseCanExecuteChanged();
}
private bool CanUpdateAms()
{
foreach (var cao in CustomAMSOffices)
{
if (!cao.Check() || cao.HasErrors)
{
return false;
}
}
foreach (var cac in CustomAMSContacts)
{
if (!cac.Check() || cac.HasErrors)
{
return false;
}
}
return true;
}
编辑:我使用的中继命令:https://github.com/briannoyes/WPFMVVM-StarterCode/blob/master/ZzaDashboard/ZzaDashboard/RelayCommand.cs
最佳答案
好的,我只是要复制粘贴一些我正在使用的代码,这样您就可以将它们弹出到您的项目中并使用。
首先,RelayCommand()
类。我从 this msdn page 中提取了这段代码:
public class RelayCommand : ICommand
{
#region Fields
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
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
}
现在我们的 ModelView.cs
类需要继承自 INotifyPropertyChanged
并且将拥有我们的 RaisePropertyChanged()
。现在我通常只是把它做成它自己的文件,让我所有的 ModelViews 从它继承,所以代码更干净一些,但你可以随心所欲。
下面是我的设置方式:
基础 View 模型.cs:
public class BaseViewModel : INotifyPropertyChanged
{
internal void RaisePropertyChanged(string prop)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(prop));
}
public event PropertyChangedEventHandler PropertyChanged;
// Any other code we want all model views to have
}
现在对于我们的 MainViewModel.cs,我们将从 BaseViewModel 继承,添加我们的事件处理程序,然后运行它!
例子:ServerViewModel.cs
public class ServerViewModel : BaseViewModel
{
public RelayCommand BroadcastMessageCommand { get; set; }
private string _broadcastmessage;
public string broadcastmessage
{
get { return _broadcastmessage; }
set { _broadcastmessage = value; RaisePropertyChanged("broadcastmessage"); }
}
Server server;
public ServerViewModel()
{
server = new Server();
server.run();
BroadcastMessageCommand = new RelayCommand(BroadcastMessage, CanBroadcast);
}
private bool CanBroadcast(object param)
{
if (string.IsNullOrWhiteSpace(broadcastmessage))
return false;
if (!server.running)
return false;
return true;
}
public void BroadcastMessage(object param)
{
server.BroadcastMessage(broadcastmessage);
broadcastmessage = "";
RaisePropertyChanged("broadcastmessage");
}
}
现在 MainView.xaml 中与 Command="{Binding broadcastmessage}"
绑定(bind)的任何内容都将适当更新。在我的例子中,我将它绑定(bind)到一个按钮,如果消息为空,或者如果我们没有连接到服务器,该按钮将被禁用。
希望这些代码示例足以让您朝着正确的方向前进!如果您对此有任何疑问,请告诉我。
关于c# - Relaycommand ICommand.CanExecute 不触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42449723/
我必须在 WPF 应用程序中使用此命令(我真的不喜欢它,但如果我必须这样做 -> 我必须这样做): http://wpftutorial.net/DelegateCommand.html 但我在这里遇
我有一个带有文本框和按钮的表单。 当该文本框的值发生变化时,按钮命令不会调用其命令的 CanExecute 方法。 命令参数已设置但似乎没有更改。加载窗口后,该按钮保持禁用状态。 我知道绑定(bi
在演示中,我有一个按钮来切换 bool 字段 isAsking。我创建了一个只有在 isAsking==true 时才能执行的命令。 一旦我按下 Toggle 按钮,okButton.IsEnable
在我的应用程序中,我有一个命令,我只希望用户能够在它尚未运行时触发。有问题的命令绑定(bind)到 WPF 按钮,这意味着如果 CanExecute 为 false,它会自动禁用该按钮。到目前为止一切
我有一个带有按钮的简单程序,该按钮绑定(bind)到 View 模型中的 Relaycommand,如下所示。我根据某个值将 CanExecute 设置为 true(由计时器设置,您可以在下面找到详细
我在我的 WPF 应用程序中使用 MVVM 模式。我正在尝试缓存我的窗口以提高性能。为此,我从不关闭它们,只是隐藏而不是关闭。所有隐藏的窗口都存储在字典中,因此我可以重复使用它们。当窗口再次打开时,我
我正在使用@Stephen-Cleary AsyncCommand implementation在 WPF (.NET 4.0) 中,现在我试图找出如何在命令定义期间指定 CanExecute 处理程
我有一个命令,检查 CanExecute 需要花费很多时间。现在,我问我,是否可以异步运行 CanExecute 方法? 最佳答案 嗯,这是可能的。您应该使用 CanExecutedChanged。在
我无法让 RelayCommand 正确启用/禁用附加控件。 我有一个附加到按钮的 EventToCommand 元素。该命令数据绑定(bind)到 ViewModel。最初,该按钮被禁用(预期行为)
好的。这是场景。这是 WPF + MVVM (.net 4.0) 应用程序: View:有一个 DataGrid 和两个按钮 Move Up/Move Down,它们应该在 DataGrid 中向上或
这个问题已经有答案了: In Java what exactly does File.canExecute() do? (3 个回答) 已关闭 9 年前。 我正在使用此代码来测试它; public c
快速记录,这样我就不会浪费任何人的时间。从 nuget 安装 MVVMLight 时,我最终收到错误 null : The term 'null' is not recognized as the n
我将控件绑定(bind)到 DelegateCommand,但它的 CanExecute 部分无法正常工作。我正在使用 Prism 库。谁能告诉我为什么? 命令声明和实例化: public Playe
我在 XAML 中有一个数据网格和保存按钮。我有一个绑定(bind)到数据网格的 ObservableCollection。 如果我在数据网格中添加/删除一行,我应该能够启用“保存”按钮以允许用户保存
我正在尝试使用 Caliburn Micro CanExecute 方法根据是否在文本框中输入值来绑定(bind)我的保存按钮是否被禁用。 这是我的看法: 我的POCO: [ImplementPr
我有一个应用程序,它在 MVVM 架构中使用 RelayCommand。 似乎在某个时间点,CanExecute 方法不再得到正确的重新评估。 (也许安装 VS2013 的最新更新导致了这个?)。 下
我正在使用 MVVM 在 WPF 中开发应用程序,但我被 ICommand 对象困住了。 我有一个包含一些按钮的窗口,因此,我将它们绑定(bind)到 XAML 中各自的 ICommand,如下所示:
我有一个非常简单的应用程序,其中包含一个 TextBox 和一个 Button。当输入到 TextBox 中的文本长度超过 5 个字符时,该按钮将被启用。这是我的 ViewModel 的代码: pri
在我的窗口中,我有加载和保存方法的按钮。我使用 CommandBinding 并且保存按钮有一个 CanExecute 属性来防止用户在加载数据之前保存数据。 CanExecute-Methode 连
我有以下命令: StartProductionCommand = new RelayCommand(OpenConnection, CanStartProduction); private bool
我是一名优秀的程序员,十分优秀!