处理“弹出窗口"PRISM 中的 Window.xaml [英] Handling "Popup" Window.xaml in PRISM

查看:308
本文介绍了处理“弹出窗口"PRISM 中的 Window.xaml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 MVVM 和 PRISM 7 开发 WPF 应用程序.

I'm developing a WPF application with MVVM and PRISM 7.

在某些地方,我想通过单击按钮打开一个新的 Window.xaml.

At some places I'd like to open a new Window.xaml with the click of a button.

所以我创建了那个 Window.xaml 并在单击该按钮/命令时像这样调用它:

So I've created that Window.xaml and am calling it like so when clicking that button/command:

private void OnInstrumentFinderCommand()
{
    var instrumentfinder = new InstrumentFinderWindow();
    var result = instrumentfinder.ShowDialog();
    if (result == true)
    {
        // logic here...   
    }
}

效果很好.InstrumentFinderWindow 打开,我可以与之交互.

That works fine. The InstrumentFinderWindow opens up and I can interact with it.

但这打破了我想用 MVVM 实现的松散耦合.

But this breaks the loosely coupling which I want to achieve with MVVM.

我知道如何将 PRISM 与 Views 和 Modules 一起使用,但无法弄清楚我必须如何处理 Window 才能实现结果与上面的代码相同,但松散耦合.不直接从 ViewModel 中调用它.

I know how to use PRISM with Views and Modules, but cannot figure out how I have to handle the Window to achieve the same result as the code above, but loosely coupled resp. not calling it directly from within a ViewModel.

有没有办法或者我必须完全不同地处理这种情况?

Is there even a way or do I have to handle this completely different?

我只是想说明一下,我正在询问一种将 System.Windows.Window 称为 MVVM/PRISM 方式的方法.与是/否/取消"弹出对话框无关.

I just want to make clear, that I'm asking about a way to call a System.Windows.Window a MVVM/PRISM-way. Not about a "Yes/No/Cancel" popup dialog.

推荐答案

可以通过 Interaction User Experience 以 PRISM 方式调用弹出对话框.

Calling popup dialog in PRISM-way can be done with Interaction User Experience.

这种方式被彻底描述了在 PRISM 文档中 并且有很多示例如何正确执行在棱镜样本中(25-NotificationRequest、26-ConfirmationRequest、27-CustomContent、28-CustomRequest).

This way is thoroughly described in PRISM documentation and there are bunch of examples how to do it properly in prism samples (25-NotificationRequest, 26-ConfirmationRequest, 27-CustomContent, 28-CustomRequest).

在您的 ViewModel 中声明 InteractionRequest 属性:

Declare InteractionRequest<T> property in your ViewModel:

public InteractionRequest<Notification> FindInstrumentRequest { get; }

在命令中提出请求

private void OnInstrumentFinderCommand() {
    FindInstrumentRequest.Raise(
        new Notification(), 
        notification =>
        {
             //the dialog shown, logic here
        });
}

给窗口View添加交互触发器

Add interaction trigger to the window View

<UserControl ...
             xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
             xmlns:prism="http://prismlibrary.com/">

    <i:Interaction.Triggers>
        <prism:InteractionRequestTrigger SourceObject="{Binding FindInstrumentRequest}">
            <prism:PopupWindowAction IsModal="True" CenterOverAssociatedObject="True">
                <prism:PopupWindowAction.WindowContent>
                    <views:InstrumentFinderWindow />
                </prism:PopupWindowAction.WindowContent>
            </prism:PopupWindowAction>
        </prism:InteractionRequestTrigger>
    </i:Interaction.Triggers>

...

</UserControl>

通过InstrumentFinderWindowViewModel实现IInteractionRequestAware接口,并通过FinishInteraction调用完成交互.

Implement IInteractionRequestAware interface by InstrumentFinderWindowViewModel and finish interaction with FinishInteraction invoking.

public partial class InstrumentFinderWindowViewModel: UserControl, IInteractionRequestAware
{
    private void SelectInstrument()
    {
        FinishInteraction?.Invoke();
    }

    public Action FinishInteraction { get; set; }
    public INotification Notification { get; set; }
}

您可以创建NotificationRequest的派生类型,并使用它在窗口和对话框之间传递数据.

You can create derived type of NotificationRequest and use it to pass data between window and dialog.

重要补充:这种交互机制将在 PRISM 的功能版本中删除.现在正在实施处理对话的新方法:DialogService.它是 PRISM 库的预发布版本.

IMPORTANT ADDITION: This interaction mechanism will be removed in feature versions of PRISM. There is new way to handle dialog is being implemented now: DialogService. It is in pre-release version of PRISM library.

这篇关于处理“弹出窗口"PRISM 中的 Window.xaml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆