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

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

问题描述

如何在不违反 MVVM 模式规则的情况下在 WPF 中打开/关闭新窗口?
我只想模仿 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.

我已经阅读了这篇文章,但是传递参数出现错误confirmation

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

我目前使用的是prism 5.0.

I'm currently using prism 5.0.

推荐答案

您使用 Prism 7 吗?
如果是,那么现在停止阅读并转到下面的这个 Prism 7 答案
如果不是,则继续阅读

更新
导致我提出另一个答案的原因是无法将接受的答案应用到我的项目中使用 Prism 6
但是在放置原始答案(见下文)并在评论,我发现核心问题是:Prism 6 改变了一些类的命名空间,在接受的答案中使用的所有类仍然存在于 Prism 6,但在另一个 dll 和命名空间中
因此,如果您使用的是 Prism 6,则可以通过这些修改应用已接受的答案

Update
What lead me to put another answer was the inability to apply the accepted answer on my project which using the Prism 6,
but after putting the original answer (see it below) and discussing it in comments, I discovered that the core problem was: The Prism 6 changed the namespaces of some classes, all the classes which used in the accepted answer is still exists in Prism 6, but in another dlls and namespaces
So if you are using Prism 6, you can apply the accepted answer with those modifications

首先替换那些命名空间

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:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:prism="http://prismlibrary.com/"

第二次更新XAML如下

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

注意 1
确保您使用的视图(在上面的示例中 )不是窗口,否则您将收到异常.

NOTE 1
Make sure that the view you are using (in the example above <views:CustomPopupWindow>) is NOT a window, or you will receive an exception.

注意 2
如果您使用 Prism 6,这些修改是必需的.因为(正如我在下面的原始答案中所说)被接受的答案使用的 dll 在 Prism 中不推荐6.

NOTE 2
These modifications are required ONLY in case you are using Prism 6. because (As I said in the Original Answer below) the dlls which used by the accepted answer is deprecated in Prism 6.

注意 3
确保您正在引用 Prism.Wpf dll,它可能是 从 Nuget 下载.

NOTE 3
Make sure you are referencing the Prism.Wpf dll, which could be downloaded from Nuget.


原答案
接受的答案指向Prism 5,它使用这个库,它在 Prism 6 中被弃用.


Original Answer
The accepted answer is directed to the Prism 5, it uses this library which is deprecated in the Prism 6.

实际上您在问题中引用的文章 非常有帮助(至少对我而言),并且不会崩溃.

Actually the article which you reference in your question was very helpful (at least for me), and it does not crash.

我会尽量总结那篇文章.

I will try to summary that article.

视图模型

public class ViewModel : BindableBase
{
    public ViewModel()
    {
        _showWindowCommand = new DelegateCommand(ShowWindow);
        _interactionRequest = new InteractionRequest<Confirmation>();
    }

    private readonly DelegateCommand _showWindowCommand;
    private InteractionRequest<Confirmation> _interactionRequest;

    public ICommand ShowWindowCommand
    {
        get { return _showWindowCommand; }
    }

    public IInteractionRequest InteractionRequest
    {
        get { return _interactionRequest; }
    }

    private void ShowWindow()
    {
        _interactionRequest.Raise(
            new Confirmation(),
            OnWindowClosed);
    }

    private void OnWindowClosed(Confirmation confirmation)
    {
        if (confirmation.Confirmed)
        {
            //perform the confirmed action...
        }
        else
        {

        }
    }
}

XAML

<Button Command="{Binding ShowWindowCommand}" Content="Show Window" >
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="Raised" SourceObject="{Binding InteractionRequest}">
            <i:EventTrigger.Actions>
                <local:ShowWindowAction></local:ShowWindowAction>
            </i:EventTrigger.Actions>
        </i:EventTrigger>
    </i:Interaction.Triggers>
</Button>

你将需要使用这些命名空间

and you will need to use those namespaces

xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:local="clr-namespace:The namespace which contains the ShowWindowAction">

ActionTrigger

using System;
using Prism.Interactivity.InteractionRequest;
using System.Windows.Interactivity;
using System.Windows;

public class ShowWindowAction : TriggerAction<FrameworkElement>
{
    protected override void Invoke(object parameter)
    {
        InteractionRequestedEventArgs args = parameter as InteractionRequestedEventArgs;
        if (args != null)
        {
            Confirmation confirmation = args.Context as Confirmation;
            if (confirmation != null)
            {
                // Replace ParametersWindow with your own window.
                ParametersWindow window = new ParametersWindow();
                EventHandler closeHandler = null;
                closeHandler = (sender, e) =>
                {
                    window.Closed -= closeHandler;
                    args.Callback();
                };
                window.Closed += closeHandler;
                window.Show();
            }
        }
    }
}

说明

  1. 您需要 Prism.CorePrism.Wpf dll(至少)才能使此代码工作.
  2. ShowWindow 方法,会触发ShowWindowActionInvoke 方法,真正显示窗口.
  3. 您可以在 OnWindowClosed 中处理窗口的关闭,我们将其作为回调传递给 ShowWindowAction 类,当窗户真的关上了.
  1. You need Prism.Core and Prism.Wpf dlls (at least) to make this code work.
  2. ShowWindow method, will trigger the Invoke method of the ShowWindowAction, which will really show the window.
  3. you can handle the closing of the window in the OnWindowClosed, which we passed it as a callback to the ShowWindowAction class, and we called it from there when the the window really closed.

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

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