• 【WIN10】Toast 通知


    DEMO下載:http://yunpan.cn/cFSLZQf5ePeTV  访问密码 1fce

    1.顯示通知

    使用xml確定通知內容。

                string xml = "<toast lang="zh-CN">" +
                                "<visual>" +
                                    "<binding template="ToastGeneric">" +
                                        "<text>Toast通知测试</text>" +
                                        "<text>来个美女</text>" +
                                        "<image placement="appLogoOverride" src="ms-appx:///Assets/ps.png" />" +
                                        "<text>背景是PS的。</text>" +
                                    "</binding>" +
                                "</visual>" +
                             "</toast>";
                // 创建XML文档
                XmlDocument doc = new XmlDocument();
                // 加载XML
                doc.LoadXml(xml);
                // 创建通知实例
                ToastNotification notification = new ToastNotification(doc);
    
                // 单击响应
                notification.Activated += OnNotification;
    
                // 显示通知
                ToastNotifier nt = ToastNotificationManager.CreateToastNotifier();
                nt.Show(notification);

    效果圖:

    2.響應單擊

    1)通過 notification.Activated += OnNotification; 代碼添加自定義響應函數:

            private void OnNotification(ToastNotification sender, object args)
            {
                ToastActivatedEventArgs trueArgs = (ToastActivatedEventArgs)args;
    
    
                // 访问界面必须使用回调
                this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, NotifiyCallback);
    
                Debug.WriteLine("[Toast] in OnNotification");
            }
    
            private void NotifiyCallback()
            {
                notifyText.Text = "您单了通知 " + clickedNotifyCount.ToString();
                ++clickedNotifyCount;
            }

    不過這種方式會有缺陷,它的args,只能是 ToastActivatedEventArgs 類型,它只能獲取到 id 值,需要有複雜的 input 框,它是無法得到用戶輸入值的。

    這個時候需要第二種方式。

    2)使用App類進行函數,重載 OnActivated 函數:

            protected override void OnActivated(IActivatedEventArgs args)
            {
                base.OnActivated(args);
    
                if (args.Kind == ActivationKind.ToastNotification)
                {
             // 保證Frame被加載 MakeSureFrameExist(args.PreviousExecutionState);
    // 转换参数类型 ToastNotificationActivatedEventArgs toastArgs = (ToastNotificationActivatedEventArgs)args; if (toastArgs.Argument == "hate" || toastArgs.Argument == "love") { var name = toastArgs.UserInput["name"]; var city = toastArgs.UserInput["city"]; // do something. } Debug.WriteLine(string.Format("[Toast] in OnActivated-args:{0}", toastArgs.Argument)); } }

    圖中紅色部分,這種處理是為了應付一種情況:

        程度關閉後,通知框並不會關閉,這個時候,用戶再去單擊通知,就會啟動程序,然後再生成Frame。

    3.XML說明。

    1)只顯示文字與圖片。

    經我試驗,在PC上,可以顯示圖片與文字,但圖片永遠顯示在最下方。

    而且在WP10(虛擬機)中,無法完全顯示圖片。

                string xml = "<toast lang="zh-CN">" +
                                "<visual>" +
                                    "<binding template="ToastGeneric">" +

    "<text
    >Toast通知测试</text>" + "<text>来个美女</text>" + "<image placement="appLogoOverride" src="ms-appx:///Assets/ps.png" />" + "<text>背景是PS的。</text>" +

    "
    </binding>" + "</visual>" + "</toast>";

    如上面的代碼,把中間的東東替換成自己的東東就行了。。第一個text為標題,顯示時會加粗。

    圖片顯示時,屬性 placement 如果是 appLogoOverride,表示它的圖標會替換默認的程序圖標。默認為inline,表示顯示在通知內容區。

    appLogoOverride:

    inline:

    2)顯示輸入控件。

    可以顯示 edit、combox、button,三種。

    我寫的例子:

    "<toast lang="zh-CN">" +
                    "<visual>" +
                        "<binding template="ToastGeneric">" +
                            "<text>Toast通知测试</text>" +
                            "<text>来个美女</text>" +
                        "</binding>" +
                    "</visual>" +

    ------- 輸入區域 --------- "<actions>" + " <input id="name" type="text" />" + ---輸入框 edit " <input id="city" type="selection">" + --- combox " <selection content="上海" id="sh" />" + " <selection content="北京" id="bj" />" + " </input>" + " <action content="hehe" arguments="hate" />" + -- button " <action content="hehe" arguments="love" imageUri="ms-appx:///Assets/ps.png" activationType="background" />" + "</actions>" +
    ------- 輸入區域 ---------

    "</toast>";

    效果圖:

    注意:

    id 在 OnActivated 中使用。

    activationType 表示在哪裡響應,foreground表示將激活應用程序到前方, 這個是默認的。如果是background表示,是後台處理,需要自己指定後台處理。

    id的使用,在OnActivated中,再發一遍:

            private void OnNotification(ToastNotification sender, object args)
            {
                ToastActivatedEventArgs trueArgs = (ToastActivatedEventArgs)args;
    
    
                // 访问界面必须使用回调
                this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, NotifiyCallback);
    
                Debug.WriteLine("[Toast] in OnNotification");
            }
    
            private void NotifiyCallback()
            {
                notifyText.Text = "您单了通知 " + clickedNotifyCount.ToString();
                ++clickedNotifyCount;
            }
    不過這種方式會有缺陷,它的args,只能是 ToastActivatedEventArgs 類型,它只能獲取到 id 值,需要有複雜的 input 框,它是無法得到用戶輸入值的。
    
    這個時候需要第二種方式。
    
    2)使用App類進行函數,重載 OnActivated 函數:
    
            protected override void OnActivated(IActivatedEventArgs args)
            {
                base.OnActivated(args);
    
                if (args.Kind == ActivationKind.ToastNotification)
                {
             // 保證Frame被加載
                    MakeSureFrameExist(args.PreviousExecutionState);
    
                    // 转换参数类型
                    ToastNotificationActivatedEventArgs toastArgs = (ToastNotificationActivatedEventArgs)args;
                    if (toastArgs.Argument == "hate" || toastArgs.Argument == "love")
                    {                   
                        var name = toastArgs.UserInput["name"];
                        var city = toastArgs.UserInput["city"];
    
                        // do something.
                    }
    
    
                    Debug.WriteLine(string.Format("[Toast] in OnActivated-args:{0}", toastArgs.Argument));
                } 
            }
    View Code

    問題:

    edit、combox並不能指定提示信息,所以實用性並不是很大,用戶很難理解這個東東是用來幹什麼的。

    在WP10(虛擬機)測試中,需要把通知框拉長才能看得到其它區域,所以這個功能也並不是非常實用,因為我認為用戶很少會去拉,除非這成為一個習慣。

    關於使用後台任務去響應,這裡不作說明了,感覺沒什麼必要,想知道的,請參看博客:http://www.cnblogs.com/tcjiaan/p/4675545.html

  • 相关阅读:
    应用默认编码不对的问题定位
    以http server为例简要分析netty3实现
    用qemu+gdb tcp server+CDT调试linux内核启动-起步
    用virtualbox+模拟串口+CDT调试linux内核 TCP/IP协议栈-起步
    【转】常见容错机制
    python文档注释参数获取
    scrapy爬取图片
    xpath语法
    python爬虫爬取赶集网数据
    爬虫小总结
  • 原文地址:https://www.cnblogs.com/lin277541/p/4898922.html
Copyright © 2020-2023  润新知