专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > WinRT Metro

【Win10开发】Toast通报——前台激活

发布时间:2011-06-23 13:55:46 文章来源:www.iduyao.cn 采编人员:星星草
【Win10开发】Toast通知——前台激活

上篇文章我们将了大体的Toast通知的模板及实例展示,那么,这篇文章就来讲讲Toast的前台激活。

首先是xaml界面,很简单,我们放一个Button和TextBlock,TextBlock用来显示Toast通知传过来的内容。

        <StackPanel VerticalAlignment="Center">
            <Button  Content="通知" VerticalAlignment="Top" HorizontalAlignment="Center"  Click="Button_Click" />
            <TextBlock Name="getInfo" Height="60" FontSize="40" HorizontalAlignment="Center" Foreground="Red" FontFamily="Microsoft YaHei"/>
        </StackPanel>

然后来构造Toast的架构。这些东西上篇文章讲了,在这里就不详述了。

            string xml = "<toast>" +
                            "<visual>" +
                                "<binding template=\"ToastGeneric\">" +
                                    "<text>通知</text>" +
                                    "<text>Toast Test</text>" +
                                    "<text>请输入您的姓名</text>" +
                                "</binding>" +
                            "</visual>" +
                            "<actions>" +
                                    "<input id=\"name\" type=\"text\" placeHolderContent=\"请输入姓名\" />" +
                                    "<action content = \"确定\" arguments = \"ok\" activationType=\"foreground\" />" +
                                    "<action content = \"取消\" arguments = \"cancel\" />" +
                            "</actions >" +
                         "</toast>";

接下来,便是让Toast通知显示出来。

            XmlDocument doc = new XmlDocument();
            doc.LoadXml(xml);
            ToastNotification notification = new ToastNotification(doc);            
ToastNotificationManager.CreateToastNotifier().Show(notification);

在这里我们先写一个ShowText方法,以便将Toast通知的参数传递到TextBlock控件的Text属性中。

        public void ShowText(string msg)
        {
            getInfo.Text = msg;
        }

 

由于应用程序是被前台激活的,所以要为Application类作激活处理,我们在App类中,重写OnActivated方法,这些代码是写在App.xaml.cs文件里面。完整代码如下,后面我们会详细讲解。

        protected override void OnActivated(IActivatedEventArgs args)
        {
//判断是否为Toast所激活 if (args.Kind == ActivationKind.ToastNotification) { // 转换参数类型 ToastNotificationActivatedEventArgs toastargs = (ToastNotificationActivatedEventArgs)args; // 获取页面引用 Frame root = Window.Current.Content as Frame; if (root == null) { root = new Frame(); Window.Current.Content = root; } if (root.Content == null) { root.Navigate(typeof(MainPage)); } MainPage page = (MainPage)root.Content; string activeargs = toastargs.Argument; if (activeargs == "ok") { // 获取用户输入的内容 string name = toastargs.UserInput["name"] as string; page.ShowText($"您的姓名是:{name}"); } else { page.ShowText("未收集到信息。"); } } Window.Current.Activate(); }

首先我们需要通过方法参数的Kind属性判断是否为Toast通知所激活,然后将方法参数转换为ToastNotificationActivatedEventArgs类型类型,此时toast的Argument属性的值就是我们在Toast XML中定义里面的arguments,值就是“ok”,“cancel”,UserInput的值就是input元素所输入的内容,是字典类型,我们可以获取到用户在文本框中输入的内容。我们还需要获取的MainPage,将Toast通知的参数传递到MainPage的TextBlock控件中。

好了,前台激活部分我们也做好的,最后当然要来看看结果。

友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: