我已经创建了一个显示Toast通知的控制台应用程序。另外,添加了对开始菜单的快捷方式,并为Toast分配了AppModelUserID。运行应用程序时将吐司,并且操作按钮会触发ToastActivated事件(按预期方式工作)。超时到期后,敬酒通知将移至“操作中心”。但是,当我尝试点击操作中心中的操作按钮以获取相同的Toast通知时,它不会触发ToastActivated事件。
遵循Is it possible to send Toast notification from console application?以获得吐司通知。
public static bool IsAwesome { get { return true; } }
public NewToastNotification()
{
string Toast = "<toast launch=\"app-defined-string\">" +
"<visual>" +
"<binding template =\"ToastGeneric\">" +
"<text> Migration Update</text>" +
"<text>" +
"Your system requires a Restart" +
"</text>" +
"</binding>" +
"</visual>" +
"<actions>" +
"<action content=\"Restart Now\" arguments=\"restart\"/>" +
"<action content=\"Dismiss\" arguments=\"dismiss\"/>" +
"</actions>" +
"</toast>";
XmlDocument tileXml = new XmlDocument();
tileXml.LoadXml(Toast);
var toast = new ToastNotification(tileXml);
toast.Activated += ToastActivated;
toast.Dismissed += ToastDismissed;
toast.Failed += ToastFailed;
string APP_ID = "Notification.KEY";
RegistryKey root = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Notifications\Settings\Notification.KEY", false);
if (root == null)
{
Registry.CurrentUser.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Notifications\Settings\Notification.KEY");
Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Notifications\Settings\Notification.KEY", true).SetValue("ShowInActionCenter", 1, RegistryValueKind.DWord);
}
// Show the toast. Be sure to specify the AppUserModelId on your application's shortcut!
ToastNotificationManager.CreateToastNotifier(APP_ID).Show(toast);
Console.ReadKey();
}
public static bool TryCreateShortcut()
{
String shortcutPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Microsoft\\Windows\\Start Menu\\Programs\\DisplayToast.lnk";
if (!File.Exists(shortcutPath))
{
InstallShortcut(shortcutPath);
return true;
}
return false;
}
private static void InstallShortcut(String shortcutPath)
{
// Find the path to the current executable
String exePath = Process.GetCurrentProcess().MainModule.FileName;
IShellLinkW newShortcut = (IShellLinkW)new CShellLink();
// Create a shortcut to the exe
ShellHelpers.ErrorHelper.VerifySucceeded(newShortcut.SetPath(exePath));
ShellHelpers.ErrorHelper.VerifySucceeded(newShortcut.SetArguments(""));
// Open the shortcut property store, set the AppUserModelId property
IPropertyStore newShortcutProperties = (IPropertyStore)newShortcut;
string APP_ID = "Notification.KEY";
using (PropVariant appId = new PropVariant(APP_ID))
{
ShellHelpers.ErrorHelper.VerifySucceeded(newShortcutProperties.SetValue(SystemProperties.System.AppUserModel.ID, appId));
ShellHelpers.ErrorHelper.VerifySucceeded(newShortcutProperties.Commit());
}
// Commit the shortcut to disk
IPersistFile newShortcutSave = (IPersistFile)newShortcut;
ShellHelpers.ErrorHelper.VerifySucceeded(newShortcutSave.Save(shortcutPath, true));
}
private static void ToastActivated(ToastNotification e, object source)
{
if (source is ToastActivatedEventArgs selectedToast)
{
if (selectedToast.Arguments.Equals("dismiss"))
{
Console.WriteLine("Bye, It's dismmissed!");
Environment.Exit(0);
}
else
{
Console.WriteLine("Let's launch an event!");
}
}
}
private static void ToastDismissed(object source, ToastDismissedEventArgs e)
{
switch (e.Reason)
{
case ToastDismissalReason.ApplicationHidden:
// Application hid the toast with ToastNotifier.Hide
Console.WriteLine("Application Hidden");
break;
case ToastDismissalReason.UserCanceled:
Console.WriteLine("User dismissed the toast");
break;
case ToastDismissalReason.TimedOut:
Console.WriteLine("Toast timeout elapsed");
break;
}
}
private static void ToastFailed(object source, ToastFailedEventArgs e)
{
// Check the error code
var errorCode = e.ErrorCode;
Console.WriteLine("Error code:{0}", errorCode);
}
static void Main(string[] args)
{
TryCreateShortcut();
NewToastNotification newToast = new NewToastNotification();
}
我希望操作中心中的Toast通知上的操作按钮会触发ToastActivated事件,但是当在操作中心中单击操作按钮时,当前操作什么也不做。