在WPF棱镜弹出新窗口 [英] Prism pop-up new window in WPF

查看:354
本文介绍了在WPF棱镜弹出新窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何开启/关闭在WPF新窗口不违反MVVM模式的规则?我只是想模仿MS Office Outlook中的登录模块。

How can I open/close a new window in WPF without violating rules of the MVVM pattern? I just want to mimic the login module of ms office outlook.

我已经阅读过的这个文章,但在传递一个参数错误确认 ..

I've already read this article, but there are an error in passing a parameter "confirmation"..

我目前使用棱镜5.0。

I'm currently using prism 5.0.

推荐答案

幸运的是,棱镜5.0(我假设6.0也没有与它没有制定),有一个叫做类 InteractionRequest< T> 您可以从代码中使用,以提高互动要求。

Luckily, Prism 5.0 (and I assume 6.0 too, haven't worked with it yet), has a class called InteractionRequest<T> which you can use from code to raise interaction requests.

这是互动的要求基本上是一个窗口,询问了用户执行特定操作和调用的回调(如果必要或需要)与用户决策/行动。

An interaction request is basically a window, that asks the user for a certain action and calls a callback (if necessary or desired) with the users decisions/actions.

public class ShellViewModel : BindableBase
{
    private readonly IRegionManager regionManager;

    public ShellViewModel(IRegionManager regionManager)
    {
        if (regionManager == null)
            throw new ArgumentNullException("regionManager");

        this.regionManager = regionManager;
        this.OptionSettingConfirmationRequest = new InteractionRequest<IConfirmation>();


        openConnectionOptionsCommand = new DelegateCommand(RaiseConnectionOptionsRequest);
    }

    public InteractionRequest<IConfirmation> OptionSettingConfirmationRequest { get; private set; }

    private readonly ICommand openConnectionOptionsCommand;
    public ICommand OpenConnectionOptionsCommand { get { return openConnectionOptionsCommand; } }

    private void RaiseConnectionOptionsRequest()
    {
        this.OptionSettingConfirmationRequest.Raise(new Confirmation { Title = "Options not saved. Do you wish to save?" }, OnConnectionOptionsResponse);
    }

    protected virtual void OnConnectionOptionsResponse(IConfirmation context)
    {
        if(context.Confirmed)
        {
            // save it
        }

        // otherwise do nothing
    }
}

在XAML中,你会做这样的事情。

In XAML you would do something like

                <Button Label="Options" Command="{Binding OpenConnectionOptionsCommand}">
                    <i:Interaction.Triggers>
                        <pit:InteractionRequestTrigger SourceObject="{Binding OptionSettingConfirmationRequest, Mode=OneWay}" >
                            <pie:LazyPopupWindowAction RegionName="ConnectionSettings" 
                                NavigationUri="ConnectionSettingsView" IsModal="True" />
                        </pit:InteractionRequestTrigger>
                    </i:Interaction.Triggers>
                </Button>



我用我自己的 PopupWindowAction 的实现(看到GitHub的项目页它的实现)名为 LazyPopupWindowAction ,这将实例化单击嵌入 ConnectionSettingsView 查看。如果你不关心你的观点被实例化一次,随意使用 PopupWindowAction ,那么它会在同一时间包含操作查看实例化。

I used my own implemetation of PopupWindowAction (see github project page for it's implementation) called LazyPopupWindowAction, which will instantiate the embedded ConnectionSettingsView View on click. If you don't care that your view is instantiated only once, feel free to use PopupWindowAction, then it will be instantiated at the same time as the View containing the action.

这基本上复制和放大器;从我的项目之一切割一些无用线粘贴。我用 IConfirmation INotification 接口,而不是具体实现。

It's basically copy & paste with cutting some useless lines from one of my projects. I used IConfirmation and INotification interfaces instead of the concrete implementations.

XAML与 PopupWindowAction

<Button Label="Options" Command="{Binding OpenConnectionOptionsCommand}">
    <i:Interaction.Triggers>
        <pit:InteractionRequestTrigger SourceObject="{Binding OptionSettingConfirmationRequest, Mode=OneWay}" >
            <pi:PopupWindowAction>
                <pi:PopupWindowAction.WindowContent>
                    <views:CustomPopupView />
                </pi:PopupWindowAction.WindowContent>
            </pi:PopupWindowAction>
        </pit:InteractionRequestTrigger>
    </i:Interaction.Triggers>
</Button>



空间声明

Namespace declarations

    xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
    xmlns:pi="clr-namespace:Microsoft.Practices.Prism.Interactivity;assembly=Microsoft.Practices.Prism.Interactivity"
    xmlns:pit="clr-namespace:Microsoft.Practices.Prism.Interactivity.InteractionRequest;assembly=Microsoft.Practices.Prism.Interactivity"
    xmlns:pie="clr-namespace:MyProject.UI.Prism.Interactivity;assembly=MyProject.UI"

更新:
由于人们不断地问关于 LazyPopupWindowAction ,我已经把源中的 GitHub上吉斯特。基本上它的基础上, PopupWindowAction 从Prims 5(和棱镜,具有不与棱镜6尚未进行测试,可能不会运行在W / O调整),并执行同样的事情,而且还增加了地区和导航支持,在打开的窗口,这东西我需要在我的应用程序。

Update: Since people keep asking about the LazyPopupWindowAction, I've put the source in a GitHub Gist. Basically it's based on the PopupWindowAction from Prims 5 (and for Prism, haven't test it with Prism 6 yet, probably won't work w/o adjustments) and does the exact same thing, but also adds Region and Navigation support with the opened window, something that I needed in my application.

一件事,我不喜欢有关默认实现了,那视图和它的视图模型将在同一时间得到壳牌实例实例化视图模型仍然在它的状态,当您关闭它(它实际上只是隐藏)。

One thing I disliked about the default implementation was, that the view and it's viewmodel will be instantiated at the same time the Shell gets instantiated and the ViewModel remains in it's state, when you close it (it was actually just hidden).

这篇关于在WPF棱镜弹出新窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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