NEWS

[DEVEXPRESS] Hướng dẫn thêm Button và Control vào Toast Notification Manager C#

[DEVEXPRESS] Hướng dẫn thêm Button và Control vào Toast Notification Manager C#
Đăng bởi: Thảo Meo - Lượt xem: 4717 09:52:58, 13/01/2020DEVEXPRESS   In bài viết

Xin chào các bạn, bài viết hôm nay mình sẽ tiếp tục hướng dẫn các bạn cách thêm button và show form data và get giá trị từ Toast Notification Manager Devexpress C#.

Trong bài viết trước, mình cũng đã có bài hướng dẫn hiển thị thông báo Notification trên Windows 10.

Tuy nhiên, bài này mình sẽ hướng dẫn nâng cao thêm một tí, là cách hiển thị form trên notification và thêm button: send, dissmiss.

Khi bấm vào nút Send thì data sẽ lấy data từ thông báo notification về Winform.

Dưới đây là giao diện demo Toast Notification Manager Devexpress c#:

toast_button_devexpress

Source code Toast Notification Devexpress C#:

using DevExpress.XtraBars.ToastNotifications;
using SimpleBroker;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;

namespace Notification
{

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            toastNotificationsManager1.RegisterApplicationActivator(typeof(ToastNotificationActivatorCustom));
            this.FormClosed += XtraForm1_FormClosed;
            Load += Frm_Main_Load;
            Closed += Form_Main_Closed;


        }

        private void Form_Main_Closed(object sender, EventArgs e) => this.Unsubscribe<Message>();
        private void Frm_Main_Load(object sender, EventArgs e) => this.Subscribe<Message>(OnNext);
        private void OnNext(Message value)
        {
            richTextBox1.BeginInvoke(new Action(() =>
            {
                richTextBox1.Text += "=============================================================" + Environment.NewLine;
                richTextBox1.Text += $"{value.Data}";
                richTextBox1.Text += "=============================================================" + Environment.NewLine;
            }));
        }
   


        private void XtraForm1_FormClosed(object sender, FormClosedEventArgs e)
        {
            toastNotificationsManager1.UnregisterApplicationActivator();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            toastNotificationsManager1.ShowNotification(toastNotificationsManager1.Notifications[0]);

        }

        private void toastNotificationsManager1_UpdateToastContent(object sender, DevExpress.XtraBars.ToastNotifications.UpdateToastContentEventArgs e)
        {
            XmlDocument content = e.ToastContent;
            XmlElement toastElement = content.GetElementsByTagName("toast").OfType<XmlElement>().FirstOrDefault();
            toastElement.SetAttribute("launch", "performAction");
            XmlElement actions = content.CreateElement("actions");
            toastElement.AppendChild(actions);
            XmlElement text = content.CreateElement("input");
            XmlElement text2 = content.CreateElement("input");
            //Input Box 
            actions.AppendChild(text);
            text.SetAttribute("id", "FirstName");
            text.SetAttribute("type", "text");
            text.SetAttribute("placeHolderContent", "Enter a First Name");

            //Input Box 
            actions.AppendChild(text2);
            text2.SetAttribute("id", "LastName");
            text2.SetAttribute("type", "text");
            text2.SetAttribute("placeHolderContent", "Type a Last Name");

            //Time selector 
            XmlElement input = content.CreateElement("input");
            actions.AppendChild(input);
            input.SetAttribute("id", "Age");
            input.SetAttribute("type", "selection");
            input.SetAttribute("defaultInput", "18Age");
            XmlElement selection = content.CreateElement("selection");
            input.AppendChild(selection);
            selection.SetAttribute("id", "18Age");
            selection.SetAttribute("content", "18 Age");
            selection = content.CreateElement("selection");
            input.AppendChild(selection);
            selection.SetAttribute("id", "20Age");
            selection.SetAttribute("content", "20 Age");

            XmlElement action = content.CreateElement("action");
            //Send button 
            actions.AppendChild(action);
            action.SetAttribute("content", "Send");
            action.SetAttribute("arguments", "Send");          
            //Close button 
            action = content.CreateElement("action");
            actions.AppendChild(action);
            action.SetAttribute("content", "Close");
            action.SetAttribute("arguments", "lose");
        }
      
    }

    [Guid("faf7a0e2-d33b-46fb-8b40-6eb35e31192a"), ComVisible(true)]
    public class ToastNotificationActivatorCustom : DevExpress.XtraBars.ToastNotifications.ToastNotificationActivator
    {
        public override void OnActivate(string arguments, Dictionary<string, string> data)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine(arguments);
            foreach (string key in data.Keys)
            {
                sb.AppendLine(string.Format("{0} = {1}", key, data[key]));
            }
          //  MessageBox.Show(sb.ToString());

            var message = new Message
            {
                Data = sb.ToString()               
            };
            message.Publish();

        }

    }

    public class Message
    {
        public string Data { get; set; }
    }
}

Thanks for watching!

 

DOWNLOAD SOURCE

 

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

[DEVEXPRESS] Hướng dẫn thêm Button và Control vào Toast Notification Manager C#
Đăng bởi: Thảo Meo - Lượt xem: 4717 09:52:58, 13/01/2020DEVEXPRESS   In bài viết

CÁC BÀI CÙNG CHỦ ĐỀ

Đọc tiếp
.