c# – WPF中的Prism弹出新窗口

如何在不违反MVVM模式规则的情况下在WPF中打开/关闭新窗口?我只是想模仿ms office outlook的登录模块.

我已经阅读了this article,但传递参数确认时出错

我目前正在使用棱镜5.0.

最佳答案
更新
让我另外回答的是无法在我使用Prism 6的项目中应用已接受的答案,但在提出原始答案(见下文)并在评论中讨论之后,我发现核心问题是: Prism 6更改了某些类的名称空间,在接受的答案中使用的所有类仍然存在于Prism 6中,但在另一个dll和名​​称空间中
因此,如果您使用Prism 6,则可以将接受的答案应用于这些修改

首先替换那些名字

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
确保您使用的视图(在上面的示例中< views:CustomPopupWindow>)不是窗口,否则您将收到异常.

笔记2
只有在使用Prism 6时才需要进行这些修改.因为(正如我在下面的原始答案中所说),Prism 6中不推荐使用已接受答案使用的dll.

注3
确保你引用的是Prism.Wpf dll,它可能是downloaded from Nuget.

原始答案
接受的答案是针对Prism 5,它使用的是this library,它在Prism 6中被弃用.

实际上the article which you reference在你的问题中非常有帮助(至少对我而言),并且它不会崩溃.

我会尝试总结那篇文章.

视图模型

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>

并且您将需要使用这些名称空间

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();
            }
        }
    }
}

说明

>你需要Prism.Core和Prism.Wpf dll(至少)使这段代码有效.
> ShowWindow方法,将触发ShowWindowAction的Invoke方法,这将真正显示窗口.
>你可以在OnWindowClosed中处理窗口的关闭,我们将它作为回调传递给ShowWindowAction类,当窗口真正关闭时我们从那里调用它.

转载注明原文:c# – WPF中的Prism弹出新窗口 - 代码日志