阅读更多有关真正有趣的Windows 10新功能的信息后, 我找到了如何在桌面上创建简单的Toastbar通知的代码段。
【使用C#在Windows 10(控制中心样式)中创建通知】该示例非常简单, 在单击按钮时, 如果发生吐司事件, 标签将显示被调用动作的名称, 源代码如下:
using System;
using System.Diagnostics;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using Windows.UI.Notifications;
using Windows.Data.Xml.Dom;
namespace DesktopToastsSample{public partial class MainWindow : Window{private const String APP_ID = "Microsoft.Samples.DesktopToastsSample";
public MainWindow(){InitializeComponent();
ShowToastButton.Click += ShowToastButton_Click;
}
// Create and show the toast.// See the "Toasts" sample for more detail on what can be done with toastsprivate void ShowToastButton_Click(object sender, RoutedEventArgs e){
// Get a toast XML templateXmlDocument toastXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastImageAndText04);
// Write text exampleXmlNodeList stringElements = toastXml.GetElementsByTagName("text");
for (int i = 0;
i <
stringElements.Length;
i++){stringElements[i].AppendChild(toastXml.CreateTextNode("This is the line #" + i));
}
// Specify the absolute path to an imageString imagePath = "file:///" + Path.GetFullPath("toastImageAndText.png");
XmlNodeList imageElements = toastXml.GetElementsByTagName("image");
imageElements[0].Attributes.GetNamedItem("src").NodeValue = http://www.srcmini.com/imagePath;
// Create the toast and attach event listenersToastNotification toast = new ToastNotification(toastXml);
toast.Activated += ToastActivated;
toast.Dismissed += ToastDismissed;
toast.Failed += ToastFailed;
// Show the toast. Be sure to specify the AppUserModelId on your application's shortcut!ToastNotificationManager.CreateToastNotifier(APP_ID).Show(toast);
}
private void ToastActivated(ToastNotification sender, object e){Dispatcher.Invoke(() =>
{Activate();
Output.Text = "The user activated the toast.";
});
}
private void ToastDismissed(ToastNotification sender, ToastDismissedEventArgs e){String outputText = "";
switch (e.Reason){case ToastDismissalReason.ApplicationHidden:outputText = "The app hid the toast using ToastNotifier.Hide";
break;
case ToastDismissalReason.UserCanceled:outputText = "The user dismissed the toast";
break;
case ToastDismissalReason.TimedOut:outputText = "The toast has timed out";
break;
}
Dispatcher.Invoke(() =>
{Output.Text = outputText;
});
}
private void ToastFailed(ToastNotification sender, ToastFailedEventArgs e){Dispatcher.Invoke(() =>
{Output.Text = "An error ocurred";
});
}}}
你可以在此处阅读有关此代码段的更多信息, 并在此处阅读有关msdn的文章。
推荐阅读
- 在WinForms中使用EasyTabs创建具有Chrome样式选项卡的C#应用??程序
- CS201 分配
- CSc 360 简单文件系统
- Android上点击EditText将不会显示软键盘
- 如何在iPhone或Android上打开WhatsApp打开数字,如+55(51)9988-7766
- 如何使用普通的POST请求将文件发送到apps脚本()
- 如何在Android中获取语言列表
- Android 7本地化问题
- Android - 显示不同日期格式的月份名称