wpf MVVM 中的简单事件处理

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1926176/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 21:21:15  来源:igfitidea点击:

Simple Event Handling in MVVM

wpfxamlmvvmbinding

提问by Chris Nicol

Just wondering what people had for ideas on how best to handle events in a ViewModel from controls on a View ... in the most lightweight way possible.

只是想知道人们对如何最好地从 View 上的控件处理 ViewModel 中的事件有什么想法......以最轻量级的方式。

Example:

例子:

<MediaElement
     MediaOpened={Binding SomeEventHandler} />

In this case we want to handle the MediaOpened event in a ViewModel. Without a framework like Prism, how would one bind this to a ViewModel?

在这种情况下,我们希望在 ViewModel 中处理 MediaOpened 事件。如果没有像 Prism 这样的框架,如何将它绑定到 ViewModel?

回答by kiwipom

Commanding- your 'SomeEventHandler' needs to be a class that implements ICommand... there's a heap of literature available online...

命令- 你的“SomeEventHandler”需要是一个实现的类ICommand......网上有很多文献......

Also - I would consider getting a free, lightweight 'mini' MVVM framework, such as MvvmFoundation, which provides the RelayCommand for just such a purpose (without the complexity/overhead of learning PRISM)

另外 - 我会考虑获得一个免费的、轻量级的“迷你” MVVM 框架,例如MvvmFoundation,它为这样的目的提供 RelayCommand(没有学习 PRISM 的复杂性/开销)

EDIT:

编辑:

Have a look at thisblog for attaching command to any event... It is incredibly powerful, as I mentioned, but I guess you do need to make a judgement call if this is what you want, compared with something like attaching an old-fashioned event, and using a super-slim event handler in your code behind that simply invokes some method on your ViewModel, something like:

看一看这个将命令附加到任何事件的博客......正如我所提到的,它非常强大,但我想如果这是你想要的,与附加一个旧的相比,我想你确实需要做出判断——时尚事件,并在您的代码中使用超薄事件处理程序,它只是调用您的 ViewModel 上的某些方法,例如:

public void SomeEventHandler(object sender, SomeEventArgs e)
{
    MyViewModel vm = (MyViewModel)this.DataContext;
    vm.HandleLoadEvent( );
}

pragmatic vs Best-practise... I'll leave it with you ;)

务实与最佳实践......我会留给你;)

回答by Thomas Levesque

Have a look at Marlon Grech's Attached Command Behaviors. It makes it easy to bind events to ViewModel commands

看看 Marlon Grech 的附加命令行为。它可以轻松地将事件绑定到 ViewModel 命令

回答by jbtule

The small and opensource ImpromptuInterface.MVVMframework ha an event binding syntaxand using the dlr in .net 4.0. Although this example requires subclassing the ImpromptuViewModel. The event binding propertydoesn't have any dependency on that specific viewmodel type and the eventbinding providerinvolved is public.

小型开源ImpromptuInterface.MVVM框架具有事件绑定语法并使用 .net 4.0 中的 dlr。尽管此示例需要继承 ImpromptuViewModel。事件绑定属性对该特定视图模型类型没有任何依赖性,并且所涉及的事件绑定提供程序是公开的。

 <Window x:Class="MyProject.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:MVVM="clr-namespace:ImpromptuInterface.MVVM;assembly=ImpromptuInterface.MVVM" Title="MainWindow" Height="600" Width="800">

  <MediaElement MVVM:Event.Bind="{Binding Events.MediaOpened.To[MediaElement_MediaOpened]}"  />

...

...

public class MyViewModel{

    public dynamic Events
    {
        get { return new EventBinder(this); }
    }

    public void MediaElement_MediaOpened(MediaElement sender,   RoutedEventArgs e){
         ...
    }
}

回答by Doug Ferguson

MediaOpened is an event and does not support command binding.

MediaOpened 是一个事件,不支持命令绑定。

In order to bind to the event a helper object may be used which attaches to the event and executes a command.

为了绑定到事件,可以使用附加到事件并执行命令的辅助对象。

To bind to the view model add a property which implements ICommand. Figure 3 in this MSDN magazine article shows the RelayCommand which is a useful implementation of ICommand. RelayCommand is initialized with a delegate to connect to your view model.

要绑定到视图模型,请添加一个实现 ICommand 的属性。这篇 MSDN 杂志文章中的图 3 显示了 RelayCommand,它是 ICommand 的一个有用实现。RelayCommand 使用委托进行初始化以连接到您的视图模型。

http://msdn.microsoft.com/en-us/magazine/dd419663.aspx

http://msdn.microsoft.com/en-us/magazine/dd419663.aspx