2017-03-09 67 views
2

如何在使用Microsoft.Toolkit.Uwp.Notifications库的WPF应用程序的系统吐司通知上将click事件附加到按钮上?WPF上的系统通知

我知道我需要使用IBackground接口,但我不能绑定到这个类,因为下面的代码导致错误(ToastNotificationActionTrigger()元素未找到,HRESULT:0x80070490)。

 private void RegisterBackgroundTask() 
     { 
     const string taskName = "ToastBackgroundTask"; 
     // Otherwise create the background task 
     var builder = new BackgroundTaskBuilder(); 
     builder.Name = taskName; 
     builder.TaskEntryPoint = typeof(ToastNotificationBackgroundTask).FullName; 
     // And set the toast action trigger 
     builder.SetTrigger(new ToastNotificationActionTrigger()); 
     // And register the task 
     builder.Register(); 
     } 

我通知的观点:

View of my notification

请帮助。

回答

0

我用通知在WPF一年前,这里是我如何实现它:

首先,你必须从增加的NuGet包Windows API Code Pack

然后我用下面的代码:

public static void RaiseGeneratedNotification(int errors, int warnings) 
{ 
    // Get a toast XML template 
    XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastImageAndText04); 

    // Fill in the text elements 
    XmlNodeList stringElements = toastXml.GetElementsByTagName("text"); 
    stringElements[0].AppendChild(toastXml.CreateTextNode("Web Studio")); 
    stringElements[1].AppendChild(toastXml.CreateTextNode(Strings.Errors+": "+errors)); 
    stringElements[2].AppendChild(toastXml.CreateTextNode(Strings.Warnings+": " +warnings)); 


    // Specify the absolute path to an image 
    String imagePath = "file:///" + Path.GetFullPath("App.png"); 
    XmlNodeList imageElements = toastXml.GetElementsByTagName("image"); 
    imageElements[0].Attributes.GetNamedItem("src").NodeValue = imagePath; 

    // Create the toast and attach event listeners 
    ToastNotification toast = new ToastNotification(toastXml) 
    { 
     ExpirationTime = DateTimeOffset.Now.AddMinutes(2) 
    }; 

    // Show the toast. Be sure to specify the AppUserModelId on your application's shortcut! 
    ToastNotificationManager.CreateToastNotifier(Resources.AppId).Show(toast); 
} 

有了这个代码,我做了以下步骤:

  1. 获得通知模板。
  2. 获得通知
  3. 领域满山遍野
  4. 创建模板
  5. 通知抬起通知

您还可以在应用程序的快捷方式来注册一个AppID。

我希望这可以帮助你。

+0

谢谢,@ ganchito55,我已经现在如何显示简单的系统通知与文字,描述和图像,但现在我想实现按钮通知。我使用了[以下文章](https://blogs.msdn.microsoft.com/tiles_and_toasts/2015/07/08/quickstart-sending-a-local-toast-notification-and-handling-activations-from-it-it- windows-10 /),但我无法处理点击按钮事件。 –

+0

@VitaliiKulyk我刚刚关注了那篇文章,并且在一个新的WPF应用程序中,我遇到ToastNotification无法识别的错误。你能用你的代码给我一个项目吗?我会尽力解决它。 – ganchito55

+0

mr @ ganchito55,你可以点击[链接](https://github.com/vitcool/WPFDesctopNotifications/tree/master/CSharp-Toast-Example)下载我的项目。我会感谢任何帮助。 –