Csharp/C#教程:命令MVVM(WPF) – 如何返回值?分享


命令MVVM(WPF) – 如何返回值?

我一直在使用MVVM模式一段时间,但我仍然在现实生活中遇到问题。 这是另一个:我使用命令和冒泡事件在ViewModel中处理。 到现在为止还挺好。 但是我使用MVVM的项目实际上是一个类库。 一旦我运行命令代码,我需要能够将一个对象发送回调用应用程序。 建议的方法是什么?

具体来说:在我的调用应用程序中,我有一个XAML页面直接绑定到库的ViewModel,它包含一个对象“Thing1”。 单击按钮时,将调用ViewModel中的方法(将其称为“CopyThing1()”)。 它复制“Thing1”以创建“Thing2”。 然后我需要将“Thing2”发送回调用应用程序。

谢谢!!!

命令不返回值,它们会更改应用程序的状态。 如果ICommands附加到ViewModels,它非常简单,因为您可以通过在执行命令时改变ViewModel来实现这一点。

使用Josh Smith出色的MVVM文章中的RelayCommand:

public class MyViewModel : INotifyPropertyChanged { private readonly ICommand mutateCommand; private Thing thing; public MyViewModel() { this.mutateCommand = new RelayCommand(this.Mutate); } public ICommand MutateCommand { get { return this.mutateCommand; } } public Thing Thing { get { return this.thing; } set { this.thing = value; // raise PropertyChanged event here... } } private void Mutate(object parameter) { this.Thing = new Thing(); } } 

在调用myVM.MutateCommand.Execute(new object()); 您可以访问myVM.Thing的新值。

如果Thing2是您的viewmodel上的另一个属性,则可以使用普通的INotifyPropertyChanged来通知UI更改。

你也可以使用Prism的EventAggregator来实现更加分离的方法

虽然关于命令的信息是清楚和正确的,但它无法在我的情况下应用,因为需要发生的响应是在一个不使用MVVM的调用应用程序中,它不是仅仅是UI响应。 我对Prism进行了调查,但发现它太复杂了,我现在还不需要。 我最终提升并处理事件,如此处所述 – > WPF MVVM从ViewModel查看Fire View事件的正确方法

理想的方法是定义一个从ICommandinheritance的新类,如下所示:

 public abstract class Command2 : ICommand { public abstract T2 Execute2(T1 parameter); } public class DelegateCommand2 : Command2 { public DelegateCommand2(Func execute, Predicate canExecute) { _execute = execute; _canExecute = canExecute; } public override T2 Execute2(T1 parameter) { if (CanExecute(parameter) == false) return default(T2); else return _execute((T1)parameter); } } 

请注意,Execute2返回的值就像普通函数一样。 这是如何使用它。

  private readonly ICommand _commandExample = new DelegateCommand2( commandExample_Executed, commandExample_CanExecute ); public Command2 CommandExample { get { return (Command2) _commandExample; } } private static Point3D commandExample_Executed(int index) { return Fun1(index); //Fun1 returns a Point_3D } private static bool commandExample_CanExecute(int index) { return true; } 

调用Execute2而不是Execute将返回该值。

上述就是C#学习教程:命令MVVM(WPF) – 如何返回值?分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/1014788.html

(0)
上一篇 2021年12月31日
下一篇 2021年12月31日

精彩推荐