MVVM中轻松实现Command绑定(四)获取事件参数EventArgs

16 篇文章 5 订阅
3 篇文章 1 订阅

从原则上说ViewModel中不应该获取View的信息,但是事实是在特殊场合需要,比如正确提交后需要关闭判断,如果ViewModel+View的后置代码实现比较麻烦,希望在ViewModel中一并完成。我在网上看到MVVM Light框架中有一种方法可以实现,但必须使用它的dll,为了尽量少用第三方dll的情况下。我使用了自己的方式来实现。 OK,现在就介绍一下我的经验。

MVVM中轻松实现Command绑定(三)中,我介绍了一种万能Command绑定,我们就利用这个进行扩展。现在,就实现关闭窗口时Window.Closing事件的额外参数中允许取消关闭。

实现思路:
1.在窗体加载时,将Window作为参数传递到ViewModel中;
2.在ViewModel中添加Window的Closing事件,此时就能得到EventArgs参数;
3.当窗体触发Closing事件时,ViewModel中定义的事件处理方法就能起效。

步骤:
1.Window窗体的xaml,注意<i:Interaction>需要使用Blend的System.Windows.Interactivity.dll文件。在Loaded事件中传递了window参数,该参数是<Window>的Name或者x:Name。

<Window x:Class="WpfApplication1.Window3_EventArgs"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
		xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
		xmlns:vm="clr-namespace:WpfApplication1"
        Title="Window3_EventArgs" Height="300" Width="300"
		x:Name="window">
	<Window.DataContext>
		<vm:Window3_ViewModel />
	</Window.DataContext>
	<Grid>
		<TextBlock Text="请点击关Closing" />
	</Grid>
	<i:Interaction.Triggers>
		<i:EventTrigger EventName="Loaded">
			<i:InvokeCommandAction Command="{Binding LoadedCommand}" CommandParameter="{Binding ElementName=window}" />
		</i:EventTrigger>
	</i:Interaction.Triggers>
</Window>


2.ViewModel,注意DelegateCommand需要Prism中的Microsoft.Practices.Prism.dll。在LoadedCommand中给win添加Closing事件。
注:Closing事件我采用的Lambda委托的语法,sender和e分别是该事件的两个参数,该事件完整签名是void win_Closing(object sender, System.ComponentModel.CancelEventArgs e) 。当然如果你需要的话,可以写成方法。

public class Window3_ViewModel {
	public ICommand LoadedCommand {
		get {
			return new DelegateCommand<Window>(win => {
				win.Closing += (sender, e) => {
					if (MessageBox.Show("确认要关闭窗口吗?", "提示", MessageBoxButton.YesNo) == MessageBoxResult.No) {
						e.Cancel = true;
					}
				};
			});
		}
	}
}


运行效果如图:

  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
在使用Prism.Mvvm进行绑定事件时,可以使用以下方法来接收参数: 1. 使用CommandParameter属性传递参数:在View使用CommandParameter属性传递参数,并在ViewModel使用CommandParameter属性接收参数。 例如,在View定义一个名为"SayHelloButton"的按钮: ```xaml <Button x:Name="SayHelloButton" Content="Say Hello" Command="{Binding SayHelloCommand}" CommandParameter="Prism.Mvvm" /> ``` 在ViewModel使用CommandParameter属性接收参数: ```csharp public ICommand SayHelloCommand { get; set; } public MyViewModel() { SayHelloCommand = new DelegateCommand<string>(SayHello); } private void SayHello(string name) { MessageBox.Show($"Hello, {name}!"); } ``` 点击按钮时,将会弹出"Hello, Prism.Mvvm!"的消息框。 2. 使用EventArgs参数传递参数:在View使用EventToCommand属性绑定事件,并在ViewModel使用EventArgs参数接收参数。 例如,在View定义一个名为"MouseDownCanvas"的Canvas: ```xaml <Canvas x:Name="MouseDownCanvas" Background="White" Width="200" Height="200"> <i:Interaction.Triggers> <i:EventTrigger EventName="MouseDown"> <i:InvokeCommandAction Command="{Binding MouseDownCommand}" /> </i:EventTrigger> </i:Interaction.Triggers> </Canvas> ``` 在ViewModel使用EventArgs参数接收参数: ```csharp public ICommand MouseDownCommand { get; set; } public MyViewModel() { MouseDownCommand = new DelegateCommand<MouseButtonEventArgs>(MouseDown); } private void MouseDown(MouseButtonEventArgs e) { MessageBox.Show($"Mouse down at ({e.GetPosition(null).X}, {e.GetPosition(null).Y})"); } ``` 在Canvas按下鼠标时,将会弹出鼠标点击位置的消息框。 这两种方法都可以实现参数的传递与接收。使用CommandParameter属性传递参数更为简单,但只能传递一个参数;使用EventArgs参数传递参数可以传递多个参数,但需要对事件进行绑定
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值