根据收到的消息致电桌面通知

时间:2019-05-02 10:00:19

标签: c# windows cefsharp

我正在使用带有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
        }
    }
}

DesktopNotifications部分:

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输出?谢谢。

0 个答案:

没有答案