WPFToastNotification单击事件

时间:2018-11-09 07:21:41

标签: c# wpf toast

我正在开发Windows应用程序,因为我需要显示通知,并且当用户单击该通知时,它应该基于参数导航到该页面。

当前,正在显示通知。我正在使用WPFNotification Nuget包来实现此目的。这是我要加载通知的代码:

INotificationDialogService _dailogService = new NotificationDialogService();

var newNotification = new Notification()
{
    Title = "New Message from ",
    Message = "123",
    ImgURL = "/icon.png",
};

var notificationConfiguration = new NotificationConfiguration(
    TimeSpan.FromSeconds(3),
    1000,
    500,
    "Notification",
    NotificationFlowDirection.RightBottom
);

_dailogService.ShowNotificationWindow(newNotification);

这是显示通知的方法。现在,我需要为该通知单击事件。我该如何实现?

1 个答案:

答案 0 :(得分:0)

要获取点击事件,您需要访问弹出的窗口。不幸的是,通过查看公共源代码可以看到,该窗口是由are collected private创建的。

但是可以通过定义自己的自定义窗口来使用公共功能WPFNotification.Core.NotifyBox.Show。您可以在代码中使用default implementation并通过单击事件订阅对其进行扩展,例如:

public static void Show(object content, NotificationConfiguration configuration)
{
    DataTemplate notificationTemplate = (DataTemplate)Application.Current.Resources[configuration.TemplateName];
    Window window = new Window()
    {
        Title = "",
        Width = configuration.Width.Value,
        Height = configuration.Height.Value,
        Content = content,
        ShowActivated = false,
        AllowsTransparency = true,
        WindowStyle = WindowStyle.None,
        ShowInTaskbar = false,
        Topmost = true,
        Background = Brushes.Transparent,
        UseLayoutRounding = true,
        ContentTemplate = notificationTemplate
    };

    //Subscribe to clicks
    window.PreviewMouseDown += NotificationWindow_PreviewMouseDoubleClick;

    WPFNotification.Core.NotifyBox.Show(
        window, configuration.DisplayDuration, configuration.NotificationFlowDirection);
}

private static void NotificationWindow_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)
{
    System.Diagnostics.Debug.WriteLine("Clicked!!!");
}

现在您可以显示类似的通知

Show(newNotification, notificationConfiguration);

请注意,您需要像getting started中所述添加模板资源,以显示默认样式的弹出窗口。

<ResourceDictionary>
  <ResourceDictionary.MergedDictionaries>
   <ResourceDictionary Source="/WPFNotification;component/Assets/NotificationUI.xaml"/>
  </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

如果您指定的模板"Notification"不可用,请使用默认模板NotificationConfiguration.DefaultConfiguration.TemplateName