c# - 从收到的消息中呼叫桌面通知
问题描述
我正在使用 CefSharp 插件开发 C# 应用程序。从此,JavaScript 函数触发了一个 C# 方法,传递数据以显示为桌面通知。我有一个 XAML 文件和显示通知的方式,但我不知道如何将仅作为字符串提供的数据传递给 DesktopNotifications 类。我是 C# 新手。
收到通知的代码:
namespace test_twenty
{
public partial class Notes: Form
{
// class variables
}
private void InitializeChromium()
{
chromiumBrowser.RegisterJsObject("callbackObj", new CallbackObjectForJs());
}
public class CallbackObjectForJs : Window
{
public void showMessage(string msg, string msg2)
{
// Here I am getting the message successfully
}
}
}
桌面通知部分:
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 toasts
private void ShowToastButton_Click(object sender, RoutedEventArgs e)
{
// Get a toast XML template
XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastImageAndText04);
// Fill in the text elements
XmlNodeList stringElements = toastXml.GetElementsByTagName("text");
for (int i = 0; i < stringElements.Length; i++)
{
stringElements[i].AppendChild(toastXml.CreateTextNode("Line " + i));
}
// Specify the absolute path to an image
String imagePath = "file:///" + Path.GetFullPath("tool_mini.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);
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 = "The toast encountered an error.";
});
}
}
}
如何将主题和文本作为 DesktopNotification 输出?谢谢你。
解决方案
推荐阅读
- typescript - 映射保留参数类型的函数数组。TypeScript v4 中的可变元组类型可以改善这一点吗?
- sql - 在 SQL 中透视数据并返回多个关联列
- javascript - 从 Python 在网页上运行 JS
- javascript - express nodejs POST请求表单输入法
- java - Spring Batch - 连接到 Postgres 数据库的问题
- php - 在 PHP 中运行一个动作函数的一个特定动作
- typescript - 如何忽略 Prettier 格式化的引号
- multithreading - 了解弱内存模型
- html - 嵌套导航栏未在主导航下对齐
- node.js - 在gremlin nodejs中的顶点之间添加边