本文介绍了WPF 的 Prism - 如何将 CancelEventArgs 发送到视图模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在窗口关闭时将 CancelEventArgs 发送到视图模型?我尝试了以下方法,但 CloseWindow 命令方法总是将 CancelEventArgs 参数接收为 null.如何以棱镜方式获取CancelEventArgs到viewmodel?

How to send CancelEventArgs to the viewmodel on window close? I tried following way, but CloseWindow command method always receive CancelEventArgs argument as null. How to get CancelEventArgs to viewmodel in prism way?

<!-- View-->
    <interactivity:Interaction.Triggers>
        <interactivity:EventTrigger EventName="Closing">
            <interactivity:InvokeCommandAction Command="{Binding CloseWindowCommand}"  />
        </interactivity:EventTrigger>
    </interactivity:Interaction.Triggers>

视图模型

CloseWindowCommand =  DelegateCommand<CancelEventArgs>.FromAsyncHandler(CloseWindow);

public async Task CloseWindow(CancelEventArgs args)
{
   //Do Stuff
}

推荐答案

你应该使用 Prism 自己的 InvokeCommandAction 类:

You should use Prism's own InvokeCommandAction class:

xmlns:prism="http://prismlibrary.com/"
...
<interactivity:Interaction.Triggers>
    <interactivity:EventTrigger EventName="Closing">
        <prism:InvokeCommandAction Command="{Binding CloseWindowCommand}" />
    </interactivity:EventTrigger>
</interactivity:Interaction.Triggers>

作为 Blend SDK 一部分的 InvokeCommandAction 类不支持将 EventArgs 作为命令参数传递.

The InvokeCommandAction class that is part of the Blend SDK doesn't support passing the EventArgs as a command parameter.

这篇关于WPF 的 Prism - 如何将 CancelEventArgs 发送到视图模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 19:40