我正在尝试创建一个原始的通知系统。
这是我显示我的窗口的方式:
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] 删除。
我来说两句