如何在队列中打开多个窗口,每个窗口等待前一个关闭?

xtl

我正在尝试创建一个原始的通知系统。

这是我显示我的窗口的方式:

var data = new NotificationData {
    Title = "New order",
    OrderId = "1"
}
var notification = new NotificationWindow(data);
    Activated += (s, e) => { notification.Owner = this; };
    notification.Show();

然后NotificationWindow只显示提供的数据。但是如果有多个通知,它们都堆叠在一个位置,我需要它们在上一个关闭后出现。

我尝试ShowDialog()改用它并且它确实有效 - 但是MainWindow当通知打开时挂起。在不同的线程中运行它Task.Run()会导致通知不出现。

杰伦·范·兰根

您需要注册到已关闭的事件,然后检查是否有另一个通知在排队。

关键是ShowOrQueueNotification检查是否存在通知。如果有一个,则将新的一个排队,当通知关闭时,您需要检查是否有一个排队。

我做了一个例子来展示如何使用队列:


主窗口.xaml

<Window x:Class="TestNotificationQueue.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:TestNotificationQueue"
        mc:Ignorable="d"
        Title="MainWindow" Height="158.868" Width="266.385">
    <Grid>
        <Border Background="Yellow" VerticalAlignment="Center" HorizontalAlignment="Center" BorderThickness="1" BorderBrush="Black" >
            <StackPanel Margin="20">
                <TextBox x:Name="TextBoxMessage" Width="192" />
                <Button Content="ShowNotification" Width="128" Height="24" Click="Button_Click" Margin="8"/>
            </StackPanel>
        </Border>
    </Grid>
</Window>

主窗口.xaml.cs

using System;
using System.Collections.Generic;
using System.Windows;

namespace TestNotificationQueue
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private Queue<NotificationInfo> _notificationQueue = new Queue<NotificationInfo>();
        private NotificationWindow _currentNotificationWindow;

        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            ShowOrQueueNotification(new NotificationInfo(TextBoxMessage.Text));
        }

        private void ShowOrQueueNotification(NotificationInfo notificationInfo)
        {
// ----->   If no notification is presented, create one.
            if (_currentNotificationWindow == null)
            {
                _currentNotificationWindow = new NotificationWindow(notificationInfo);
                _currentNotificationWindow.Closed += CurrentNotificationWindow_Closed;
                _currentNotificationWindow.Show();
            }
            else
// ----->       queue it.
                _notificationQueue.Enqueue(notificationInfo);
        }

        private void CurrentNotificationWindow_Closed(object sender, EventArgs e)
        {
// ----->   This is crucial, you need to set the current to null, else all new notification will be queued and never be presented.
            _currentNotificationWindow = null;

            if(_notificationQueue.Count > 0)
                ShowOrQueueNotification(_notificationQueue.Dequeue());
        }
    }
}

通知.cs

namespace TestNotificationQueue
{
    public class NotificationInfo
    {
        public NotificationInfo(string message)
        {
            Message = message;
        }

        public string Message { get; }
    }
}

通知窗口.xaml

<Window x:Class="TestNotificationQueue.NotificationWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:TestNotificationQueue"
        mc:Ignorable="d"
        Title="NotificationWindow" Height="93.117" Width="239.4">
    <Grid>
        <TextBlock x:Name="TextBoxMessage" HorizontalAlignment="Center" VerticalAlignment="Center" Width="200" Height="30" />
    </Grid>
</Window>

通知窗口.xaml.cs

using System.Windows;

namespace TestNotificationQueue
{
    /// <summary>
    /// Interaction logic for NotificationWindow.xaml
    /// </summary>
    public partial class NotificationWindow : Window
    {
        public NotificationWindow(NotificationInfo notificationInfo)
        {
            InitializeComponent();
            TextBoxMessage.Text = notificationInfo.Message;
        }
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

生成终端窗口的脚本突然等待每个窗口关闭,然后再打开下一个窗口

如何在React Passport Auth Flow中打开和关闭一个小窗口

PyQt5:如何在每个连接的屏幕中打开一个窗口?

如何“等待”另一个窗口的关闭

多个下拉窗口在打开另一个窗口时会自动关闭

当一个新窗口打开时如何关闭打开的模态窗口?

如何在电子 js 中创建一个新窗口,以使原始窗口在关闭新窗口之前无法使用?

当一个应用程序的多个窗口打开时,如何在 Ubuntu 18.04 中更改菜单提示?

C#等待另一个打开的窗口关闭,然后执行其余功能

如何在多个Chrome窗口中打开一个网页以进行屏幕尺寸测试

如何使用JTable打开窗口并关闭另一个窗口

传单地图:打开一个窗口时,如何阻止所有弹出窗口关闭?

如何关闭cmd窗口并打开一个新窗口以继续执行脚本

通过Tkinter中的按钮关闭窗口并打开一个新窗口

关闭实际的窗口并从ViewModel中打开一个新窗口

如何在WPF中从第一个窗口打开第二个窗口?

使用 Tkinter iwant 打开第二个窗口,然后关闭我所在的前一个窗口

使用javascript按下按钮时,如何打开一个窗口并关闭另一个窗口?

如何在pyside2中关闭另一个窗口?

如何在div中做一个关闭窗口按钮?

如何在Android中@键按下打开一个对话窗口?

如何在JavaFX 2中打开另一个窗口?

如何在GridView中打开一个链接的弹出窗口

如何在c#winform中仅打开一个窗口

如何在控制台应用程序中打开一个窗口?

如何在tkinter中使用python 3.5的滚动条打开和关闭另一个窗口?

如何打开根窗口,关闭它,然后几秒钟后在tkinter中打开另一个窗口?

如何仅打开一个窗口弹出窗口

我如何在另一个窗口内打开窗口