向交互式快速入门添加关闭确认

时间:2015-08-10 21:30:46

标签: wpf prism

Microsoft提供了交互性快速入门的示例项目,作为处理交互性请求的课程。

https://msdn.microsoft.com/en-us/library/ff921081(v=pandp.40).aspx

我正试图与Windows关闭事件联系,以确认用户确实想要关闭。这是我需要在我的应用程序中实现的东西,我使用快速启动作为一个干净的项目来计算这些细节。我在后面的MainWindow代码中添加了几行代码。

/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    // Added InteractionRequest
    public InteractionRequest<IConfirmation> CloseRequest { get; private set; }

    public MainWindow()
    {
        InitializeComponent();

        //Added the following 2 lines:
        CloseRequest = new InteractionRequest<IConfirmation>();
        Closing += OnWindowClosing;
    }

    // Added method
    private void OnWindowClosing(object sender, System.ComponentModel.CancelEventArgs e)
    {
        CloseRequest
            .Raise
            (
                new Confirmation { Content = "Are you sure you want to close?", Title = "Confirmation" },
                c =>
                {
                    e.Cancel = !c.Confirmed;
                }
            );
    }
}

我向XAML添加了交互触发器:

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

我必须仍然遗漏一些东西,因为它没有给我确认窗口。它正在点击OnWindowClosing方法和事件引发交互请求事件,但应用程序只是立即关闭。

允许确认对话的这项工作仍然缺少什么?

1 个答案:

答案 0 :(得分:0)

问题是提升事件并不会使当前线程继续进行,因此在提升事件后窗口会立即关闭。

试试这个:

private bool confirmed;

private void OnWindowClosing(object sender, System.ComponentModel.CancelEventArgs e)
    {

        if (!confirmed)
        {
        CloseRequest
            .Raise
            (
                new Confirmation { Content = "Are you sure you want to close?", Title = "Confirmation" },
                c =>
                {
                    this.confirmed = c.Confirmed;
                }
            );
            e.Cancel;
        }
    }