向Windows发送消息通知,通知中有三个按钮,如何调用这三个按钮的事件呢,有没有完整的调用方法呢?
new ToastContentBuilder()
.AddText("Test")
.AddButton(new ToastButton("按钮1", "param1").SetBackgroundActivation())
.AddButton(new ToastButton("按钮2", "param2").SetBackgroundActivation())
.Show();
ToastNotificationManagerCompat.OnActivated += args => {
if(args.Argument == "param1") {
MessageBox.Show("点击了按钮1");
} else if (args.Argument == "param2") {
MessageBox.Show("点击了按钮2");
}
};
基于new bing的调写:
首先需要明确,向 Windows 发送消息通知是针对 UWP 应用程序的。在 UWP 应用程序中,可以使用 Toast Notification 提供的 API 来构建消息通知,并且支持添加按钮来响应用户的操作。
以下是一个完整实现示例代码:
// 构建消息通知内容
var builder = new ToastContentBuilder()
.AddText("有新的版本,是否升级?")
.AddButton(new ToastButton("button1", "button1").SetBackgroundActivation())
.AddButton(new ToastButton("button2", "button2").SetBackgroundActivation())
.AddButton(new ToastButton("button3", "button3").SetBackgroundActivation());
// 显示消息通知并处理用户的操作
ToastNotificationManager.CreateToastNotifier().Show(new ToastNotification(builder.GetToastContent().GetXml()));
通过 AddButton 方法可以向消息通知中添加按钮,并且可以设置它们的事件处理程序。其中,SetBackgroundActivation() 方法表示在后台激活应用程序,也就是在应用程序没有打开的情况下也能接收到按钮事件。如果需要前台激活应用程序,则可以调用 SetForegroundActivation() 方法。
最后,通过调用 Show 方法来显示构建好的消息通知。当用户点击按钮时,系统会调用相应的事件处理程序来处理用户的操作。
直接修改浏览器的名字,这种事最简单的方法,KMS(小马激活工具)通过搜索相关的关键字。比如chrome、google、360等一些主流浏览器,对其进行动态代码注入。
csharp
using Windows.UI.Notifications;
// 创建Toast通知
ToastTemplateType toastTemplate = ToastTemplateType.ToastImageAndText2;
XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(toastTemplate);
// 设置通知内容
XmlNodeList toastTextElements = toastXml.GetElementsByTagName("text");
toastTextElements[0].AppendChild(toastXml.CreateTextNode("Notification Text"));
toastTextElements[1].AppendChild(toastXml.CreateTextNode("Secondary Notification Text"));
// 设置按钮
XmlElement actionsElement = (XmlElement)toastXml.GetElementsByTagName("actions")[0];
// 添加第1个按钮
XmlElement action1 = toastXml.CreateElement("action");
action1.SetAttribute("content", "Button 1");
action1.SetAttribute("arguments", "button1");
actionsElement.AppendChild(action1);
// 添加第2个按钮
XmlElement action2 = toastXml.CreateElement("action");
action2.SetAttribute("content", "Button 2");
action2.SetAttribute("arguments", "button2");
actionsElement.AppendChild(action2);
// 添加第3个按钮
XmlElement action3 = toastXml.CreateElement("action");
action3.SetAttribute("content", "Button 3");
action3.SetAttribute("arguments", "button3");
actionsElement.AppendChild(action3);
// 创建ToastNotification实例并显示Toast通知
ToastNotification toast = new ToastNotification(toastXml);
ToastNotificationManager.CreateToastNotifier().Show(toast);
整合chatGPT的答案如下:
在Windows中,可以使用桌面应用程序或UWP应用程序来实现发送消息通知和响应按钮事件的功能。具体步骤如下:
创建一个ToastContentBuilder对象,并添加文本和三个按钮。
ToastContentBuilder builder = new ToastContentBuilder();
builder.AddText("有新的版本,是否升级?")
.AddButton(new ToastButton("button1", "button1")).SetBackgroundActivation()
.AddButton(new ToastButton("button2", "button2")).SetBackgroundActivation()
.AddButton(new ToastButton("button3", "button3")).SetBackgroundActivation();
调用Show()方法,将Toast发送到系统通知区域。builder.Show();
实现按钮点击事件的处理逻辑。
如果是使用桌面应用程序实现的话,需要在程序中注册一个COM组件,然后通过COM接口来响应按钮事件。
具体可以参考Microsoft官方文档:https://docs.microsoft.com/en-us/windows/win32/shell/how-to-handle-toast-button-clicks-in-windows-10。
首先需要在程序中注册一个COM组件,并实现INotificationActivationCallback接口来处理按钮点击事件。具体步骤如下:
创建一个类并继承自INotificationActivationCallback接口,实现其中的Activate方法,该方法会在按钮被点击时被调用。
using System.Runtime.InteropServices;
using Windows.Data.Xml.Dom;
using Microsoft.Toolkit.Uwp.Notifications;
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
[Guid("YOUR_GUID_HERE")]
public class NotificationActivator : INotificationActivationCallback
{
public void Activate(string appUserModelId, string invokedArgs,
INotificationInput input, out int dismissal)
{
// 根据invokedArgs判断按钮名称,并进行相应的处理
switch (invokedArgs)
{
case "button1":
// 处理button1点击事件
break;
case "button2":
// 处理button2点击事件
break;
case "button3":
// 处理button3点击事件
break;
default:
// 处理未知按钮点击事件
break;
}
// 设置dismissal参数以关闭通知
dismissal = 0;
}
}
在程序入口处注册该COM组件,并使用DesktopNotificationManager类发送Toast通知。
// 注册COM组件
string shortcutPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Microsoft\\Windows\\Start Menu\\Programs\\NotificationActivator\\" + "NotificationActivator.lnk";
if (!File.Exists(shortcutPath))
{
InstallShortcut("NotificationActivator", shortcutPath,
Assembly.GetExecutingAssembly().Location, "YOUR_GUID_HERE");
}
// 发送Toast通知
XmlDocument toastXml = new XmlDocument();
toastXml.LoadXml("<toast><visual><binding template='ToastGeneric'><text>有新的版本,是否升级?</text></binding></visual>" +
"<actions><action content = 'button1' arguments = 'button1'/><action content = 'button2' arguments = 'button2'/><action content = 'button3' arguments = 'button3'/></actions></toast>");
var toast = new ToastNotification(toastXml);
toast.Activated += Toast_Activated;
DesktopNotificationManagerCompat.CreateToastNotifier().Show(toast);
// 处理Toast通知点击事件
private void Toast_Activated(ToastNotification sender, object args)
{
// 获取ActivationArguments中的invokedArgs参数,并根据不同的值进行处理
var toastArgs = args as ToastActivatedEventArgs;
if (toastArgs != null && toastArgs.Argument == "button1")
{
// 处理button1点击事件
}
else if (toastArgs != null && toastArgs.Argument == "button2")
{
// 处理button2点击事件
}
else if (toastArgs != null && toastArgs.Argument == "button3")
{
// 处理button3点击事件
}
}
如果是使用UWP应用程序实现的话,可以在App.xaml.cs文件中重写OnActivated()方法,在其中判断Toast通知是否包含自定义按钮的ActivationArguments信息,并根据不同的按钮名称进行处理。
具体可以参考Microsoft官方文档:https://docs.microsoft.com/en-us/windows/apps/design/shell/tiles-and-notifications/send-local-toast。
在UWP应用程序中,可以直接在App.xaml.cs文件中重写OnActivated()方法来处理Toast通知的点击事件和按钮点击事件。
protected override void OnActivated(IActivatedEventArgs args)
{
if (args.Kind == ActivationKind.ToastNotification)
{
var toastArgs = args as ToastNotificationActivatedEventArgs;
// 判断Toast通知是否包含自定义按钮的ActivationArguments信息,并根据不同的按钮名称进行处理
if (toastArgs.Argument == "button1")
{
// 处理button1点击事件
}
else if (toastArgs.Argument == "button2")
{
// 处理button2点击事件
}
else if (toastArgs.Argument == "button3")
{
// 处理button3点击事件
}
// 关闭Toast通知
var toastNotifier = ToastNotificationManager.CreateToastNotifier();
toastNotifier.Remove(toastArgs.Argument);
}
}
然后使用ToastNotificationManager类发送Toast通知,并在其中添加自定义按钮。
// 发送Toast通知
XmlDocument toastXml = new XmlDocument();
toastXml.LoadXml("<toast><visual><binding template='ToastGeneric'><text>有新的版本,是否升级?</text></binding></visual><actions><action content='button1' arguments='button1'/><action content='button2' arguments='button2'/><action content='button3' arguments='button3'/></actions></toast>");
var toast = new ToastNotification(toastXml);
ToastNotificationManager.CreateToastNotifier().Show(toast);
需要注意的是,为了实现按钮点击事件的处理,UWP应用程序需要在Package.appxmanifest文件中声明“uap:Extension”并设置“Category=windows.toastNotification”,如下所示:
<Package ...>
<Applications>
<Application ...>
<Extensions>
<uap:Extension Category="windows.toastNotification">
uap:ToastNotification
uap:Actions
<uap:Action ActivationType="foreground" Content="button1" Arguments="button1"/>
<uap:Action ActivationType="foreground" Content="button2" Arguments="button2"/>
<uap:Action ActivationType="foreground" Content="button3" Arguments="button3"/>
</uap:Actions>
</uap:ToastNotification>
</uap:Extension>
</Extensions>
</Application>
</Applications>
</Package>
这样就可以在UWP应用程序中响应Toast通知的按钮点击事件了。