title author date CreateTime categories
win10 uwp 活动磁贴
lindexi
2018-2-13 17:23:3 +0800
2018-2-13 17:23:3 +0800
Win10 UWP

本文翻译:https://mobileprogrammerblog.wordpress.com/2015/12/23/live-tiles-and-notifications-in-universal-windows-10-app/ 我会写很多质量很低文章,文章都是胡说,如果看不懂可以发到邮箱 如下面的图,很多应用都有活动磁贴,活动磁贴就是放在开始菜单,会像是下面图一样显示东西 win10总有很多看起来有用,但实际没什么卵用的东西,我一点不觉得用户觉得这个有用,但是我们能做活动磁贴UWP,微软一直把开发者当成用户。

做一个UWP当然需要我们打开神器

新建一个项目,空UWP,可以使用快捷键ctrl+shift+N

我们打开MainPage.xaml,新建的时候有点慢,我们需要等一下如果放在固态基本不用等。

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"><Grid.RowDefinitions><RowDefinition /><RowDefinition /></Grid.RowDefinitions><Grid><Grid.RowDefinitions><RowDefinition /><RowDefinition /></Grid.RowDefinitions><StackPanel Grid.Row="0" Margin="12"><TextBlock Text="Adaptive Tiles" FontSize="20" FontWeight="Bold" /><Button Click="UpdateBadge" VerticalAlignment="Top" Margin="12" Background="#330070B0">Update Badge Count</Button><Button Click="UpdatePrimaryTile" VerticalAlignment="Top" Background="#330070B0" Margin="12">Update Primary Tile</Button></StackPanel><StackPanel Grid.Row="1" Margin="12"><TextBlock Text="Interactive Toast" FontSize="20" FontWeight="Bold" /><StackPanel Orientation="Horizontal" Margin="12"><TextBlock x:Name="Description" VerticalAlignment="Center" Text="{x:Bind CurrentToDoTask.Description, Mode=OneWay}" FontWeight="Bold" /><CheckBox Margin="12,0,0,0" IsChecked="{x:Bind CurrentToDoTask.IsComplete, Mode=OneWay}" IsEnabled="False" /></StackPanel><Button Click="Notify" Background="#330070B0" Margin="12">Notify</Button><Button Background="#330070B0" Click="{x:Bind Refresh}" Margin="12">Refresh</Button></StackPanel></Grid><Grid Grid.Row="1"><Grid.RowDefinitions><RowDefinition /><RowDefinition /></Grid.RowDefinitions><StackPanel Grid.Row="0" Margin="12"><TextBlock Text="我翻译的 本文来自http://blog.csdn.net/lindexi_gd" /><TextBlock Text="磁贴" FontSize="20" FontWeight="Bold" /><Button Click="UpdateBadge" VerticalAlignment="Top" Margin="12" Background="#330070B0">更新磁贴数</Button><Button Click="UpdatePrimaryTile" VerticalAlignment="Top" Background="#330070B0" Margin="12">更新显示磁贴</Button></StackPanel><StackPanel Grid.Row="1" Margin="12"><TextBlock Text="互动吐司" FontSize="20" FontWeight="Bold" /><StackPanel Orientation="Horizontal" Margin="12"><TextBlock x:Name="xdescription" VerticalAlignment="Center" Text="{x:Bind CurrentToDoTask.Description, Mode=OneWay}" FontWeight="Bold" /><CheckBox Margin="12,0,0,0" IsChecked="{x:Bind CurrentToDoTask.IsComplete, Mode=OneWay}" IsEnabled="False" /></StackPanel><Button Click="Notify" Background="#330070B0" Margin="12">通知</Button><Button Background="#330070B0" Click="{x:Bind Refresh}" Margin="12">更新</Button></StackPanel></Grid></Grid>

写完我们可以看到下面的样子

上面一张是作者写的开始我没有去看,以为他写出来就是上面那图,复制了他代码在我写博客,发现他的代码错了,我自己重新写,发现我应该弄个中文,就写了第二张图,我们看到上面代码是第二张图。

我们右击方案新建一个文件夹DATA,里面新建一个类PrimaryTile,可以看下面图

我们在PrimaryTile

    public class PrimaryTile{public string time{set;get;} = "8:15 AM, Saturday";public string message{set;get;} = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed  do eiusmod tempor incididunt ut labore.";public string message2{set;get;} = "At vero eos et accusamus et iusto odio dignissimos ducimus  qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias  excepturi sint occaecati cupiditate non provident.";public string branding{set;get;} = "name";public string appName{set;get;} = "UWP";}

创建一个文件夹services 新建tileservice.cs toastservice.cs

    public class TileService{public static void SetBadgeCountOnTile(int count){// Update the badge on the real tile  System.Xml.XmlDocument badgeXml = BadgeUpdateManager.GetTemplateContent(BadgeTemplateType.BadgeNumber);XmlElement badgeElement = (XmlElement) badgeXml.SelectSingleNode("/badge");badgeElement.SetAttribute("value", count.ToString());BadgeNotification badge = new BadgeNotification(badgeXml);BadgeUpdateManager.CreateBadgeUpdaterForApplication().Update(badge);}public static XmlDocument CreateTiles(PrimaryTile primaryTile){XDocument xDoc = new XDocument(new XElement("tile", new XAttribute("version", 3),new XElement("visual",// Small Tile  new XElement("binding", new XAttribute("branding", primaryTile.branding),new XAttribute("displayName", primaryTile.appName), new XAttribute("template", "TileSmall"),new XElement("group",new XElement("subgroup",new XElement("text", primaryTile.time, new XAttribute("hint-style", "caption")),new XElement("text", primaryTile.message,new XAttribute("hint-style", "captionsubtle"), new XAttribute("hint-wrap", true),new XAttribute("hint-maxLines", 3))))),// Medium Tile  new XElement("binding", new XAttribute("branding", primaryTile.branding),new XAttribute("displayName", primaryTile.appName), new XAttribute("template", "TileMedium"),new XElement("group",new XElement("subgroup",new XElement("text", primaryTile.time, new XAttribute("hint-style", "caption")),new XElement("text", primaryTile.message,new XAttribute("hint-style", "captionsubtle"), new XAttribute("hint-wrap", true),new XAttribute("hint-maxLines", 3))))),// Wide Tile  new XElement("binding", new XAttribute("branding", primaryTile.branding),new XAttribute("displayName", primaryTile.appName), new XAttribute("template", "TileWide"),new XElement("group",new XElement("subgroup",new XElement("text", primaryTile.time, new XAttribute("hint-style", "caption")),new XElement("text", primaryTile.message,new XAttribute("hint-style", "captionsubtle"), new XAttribute("hint-wrap", true),new XAttribute("hint-maxLines", 3)),new XElement("text", primaryTile.message2,new XAttribute("hint-style", "captionsubtle"), new XAttribute("hint-wrap", true),new XAttribute("hint-maxLines", 3))),new XElement("subgroup", new XAttribute("hint-weight", 15),new XElement("image", new XAttribute("placement", "inline"),new XAttribute("src", "Assets/StoreLogo.png"))))),//Large Tile  new XElement("binding", new XAttribute("branding", primaryTile.branding),new XAttribute("displayName", primaryTile.appName), new XAttribute("template", "TileLarge"),new XElement("group",new XElement("subgroup",new XElement("text", primaryTile.time, new XAttribute("hint-style", "caption")),new XElement("text", primaryTile.message,new XAttribute("hint-style", "captionsubtle"), new XAttribute("hint-wrap", true),new XAttribute("hint-maxLines", 3)),new XElement("text", primaryTile.message2,new XAttribute("hint-style", "captionsubtle"), new XAttribute("hint-wrap", true),new XAttribute("hint-maxLines", 3))),new XElement("subgroup", new XAttribute("hint-weight", 15),new XElement("image", new XAttribute("placement", "inline"),new XAttribute("src", "Assets/StoreLogo.png"))))))));XmlDocument xmlDoc = new XmlDocument();xmlDoc.LoadXml(xDoc.ToString());//Debug.WriteLine(xDoc);  return xmlDoc;}}public static class ToastService{public static System.Xml.XmlDocument CreateToast(){XDocument xDoc = new XDocument(new XElement("toast",new XElement("visual",new XElement("binding", new XAttribute("template", "ToastGeneric"),new XElement("text", "To Do List"),new XElement("text", "Is the task complete?")) // binding  ), // visual  new XElement("actions",new XElement("action", new XAttribute("activationType", "background"),new XAttribute("content", "Yes"), new XAttribute("arguments", "yes")),new XElement("action", new XAttribute("activationType", "background"),new XAttribute("content", "No"), new XAttribute("arguments", "no"))) // actions  ));System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();xmlDoc.LoadXml(xDoc.ToString());return xmlDoc;}}

我们创建文件ToDoTask.cs ToDoTaskFileHelper.cs

    public class ToDoTask{public string Description{get;set;}public Guid Id{get;set;}public bool? IsComplete{get;set;}public string ToJson(){using (MemoryStream stream = new MemoryStream()){DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof (ToDoTask));serializer.WriteObject(stream, this);stream.Position = 0;byte[] jsonBytes = stream.ToArray();return Encoding.UTF8.GetString(jsonBytes, 0, jsonBytes.Length);}}public static ToDoTask FromJson(string json){// note: throws exception if the json is not valid  JsonObject jsonData = JsonObject.Parse(json);// exceptions will be thrown if the values do not match the types  return new ToDoTask{Id = Guid.Parse(jsonData["Id"].GetString()),Description = jsonData["Description"].GetString(),IsComplete = jsonData["IsComplete"].GetBoolean()};}}public static class ToDoTaskFileHelper{public static async Task ReadToDoTaskJsonAsync(){// declare an empty variable to be filled later  string json = null;// define where the file resides  StorageFolder localfolder = ApplicationData.Current.LocalFolder;// see if the file exists  if (await localfolder.TryGetItemAsync(Filename) != null){// open the file  StorageFile textfile = await localfolder.GetFileAsync(Filename);// read the file  json = await FileIO.ReadTextAsync(textfile);}// if the file doesn't exist, we'll copy the app copy to local storage  else{StorageFile storageFile =await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/task.json"));await storageFile.CopyAsync(ApplicationData.Current.LocalFolder);json = await FileIO.ReadTextAsync(storageFile);}return json;}public static async Task SaveToDoTaskJson(string json){StorageFolder localfolder = ApplicationData.Current.LocalFolder;StorageFile textfile = await localfolder.GetFileAsync(Filename);await FileIO.WriteTextAsync(textfile, json);}private static readonly string Filename = "task.json";}

task.json

{"Description":"A test task","Id":"9d6c3585-d0c2-4885-8fe0-f02727f8e483","IsComplete":true}

我们把刚才写的MainPage的按钮绑定到

    public sealed partial class MainPage : Page, INotifyPropertyChanged{public MainPage(){InitializeComponent();Loaded += MainPage_Loaded;}#region Delegatespublic event PropertyChangedEventHandler PropertyChanged;private void MainPage_Loaded(object sender, RoutedEventArgs e){Refresh();}#endregionpublic ToDoTask CurrentToDoTask{get{return _currentToDoTask;}set{_currentToDoTask = value;PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(CurrentToDoTask)));}}private int _count;private ToDoTask _currentToDoTask;private async void Refresh(){void json = await ToDoTaskFileHelper.ReadToDoTaskJsonAsync();CurrentToDoTask = ToDoTask.FromJson(json);}private void UpdateBadge(object sender, RoutedEventArgs e){_count++;TileService.SetBadgeCountOnTile(_count);}private void UpdatePrimaryTile(object sender, RoutedEventArgs e){XmlDocument xmlDoc = TileService.CreateTiles(new PrimaryTile());TileUpdater updater = TileUpdateManager.CreateTileUpdaterForApplication();TileNotification notification = new TileNotification(xmlDoc);updater.Update(notification);}private void Notify(object sender, RoutedEventArgs e){System.Xml.XmlDocument xmlDoc = ToastService.CreateToast();ToastNotifier notifier = ToastNotificationManager.CreateToastNotifier();ToastNotification toast = new ToastNotification(xmlDoc);notifier.Show(toast);}}

写完自己运行就可以知道,更新磁贴,更新界面,提示通知,每个对应的代码自己可以看到,这个国内很多教程

http://blog.csdn.net/lindexi_gd

https://mobileprogrammerblog.wordpress.com/2015/12/23/live-tiles-and-notifications-in-universal-windows-10-app/

2018-2-13-win10-uwp-活动磁贴相关推荐

  1. win10 uwp 使用 Microsoft.Graph 发送邮件

    在 2018 年 10 月 13 号参加了 张队长 的 Office 365 训练营 学习如何开发 Office 365 插件和 OAuth 2.0 开发,于是我就使用 UWP 尝试使用 Micros ...

  2. Win10 UWP开发中的重复性静态UI绘制小技巧 1

    Win10 UWP开发中的重复性静态UI绘制小技巧 1 原文:Win10 UWP开发中的重复性静态UI绘制小技巧 1 介绍 在Windows 10 UWP界面实现的过程中,有时会遇到一些重复性的.静态 ...

  3. 计算机与科学技术暑期社会实践,2018年暑期社会实践活动|计算机科学技术学院、软件学院...

    原标题:2018年暑期社会实践活动|计算机科学技术学院.软件学院 为了弘扬中华民族的传统美德,提升大学生的思想道德素质,推动校园精神文明的传承与建设,秉承雷锋精神的理念,对弱势群体多一些关怀与帮助.同 ...

  4. win10 UWP 应用设置

    win10 UWP 应用设置 简单的把设置需要的,放到微软自带的LocalSettings LocalSettings.Values可以存放几乎所有数据 如果需要存放复合数据,一个设置项是由多个值组成 ...

  5. win10 uwp DataContext

    本文告诉大家DataContext的多种绑法. 适合于WPF的绑定和UWP的绑定. 我告诉大家很多个方法,所有的方法都有自己的优点和缺点,可以依靠自己喜欢的用法使用.当然,可以在新手面前秀下,一个页面 ...

  6. Win10 UWP开发系列:使用VS2015 Update2+ionic开发第一个Cordova App

    安装VS2015 Update2的过程是非常曲折的.还好经过不懈的努力,终于折腾成功了. 如果开发Cordova项目的话,推荐大家用一下ionic这个框架,效果还不错.对于Cordova.PhoneG ...

  7. win10 uwp 毛玻璃

    原文:win10 uwp 毛玻璃 版权声明:博客已迁移到 http://lindexi.gitee.io 欢迎访问.如果当前博客图片看不到,请到 http://lindexi.gitee.io 访问博 ...

  8. win10 uwp 打包第三方字体到应用

    原文:win10 uwp 打包第三方字体到应用 有时候我们会把一些特殊字体打包到软件,因为如果找不到我们的字体会变为默认,现在很多字体图标我们用得好,有时候我们的应用会用很漂亮的字体,需要我们自己打包 ...

  9. 计算机系职教周方案,琼软院软件〔2018〕14 号:关于印发《软件工程系2018年“职业教育 活动周”活动方案》的通知...

    琼软院软件[2018]14号 海南软件职业技术学院软件工程系 关于印发<软件工程系2018年"职业教育 活动周"活动方案>的通知 各位老师: <软件工程系2018 ...

  10. win10 uwp 使用 Matrix3DProjection 进行 3d 投影

    win10 uwp 使用 Matrix3DProjection 进行 3d 投影 原文:win10 uwp 使用 Matrix3DProjection 进行 3d 投影 版权声明:博客已迁移到 htt ...

最新文章

  1. 以服务的方式提供站点基础功能支持
  2. 第三十三讲:tapestry Ajax eventlink无刷新页面
  3. python代做在哪找靠谱_比较靠谱的资产评估师考试去哪找
  4. 人工智能ai 学习_人工智能中强化学习的要点
  5. MongoDB基础介绍安装与使用
  6. 详解S60 WebKit 21772编译教程
  7. iOS开发 - StoryBoard + UIScrollView + UIView
  8. Spring Cloud 微服务实战系列-Spring Boot再次入门(二)
  9. 理解函数的相关概念python_Python函数的概念和使用
  10. Facebook的数据挖掘,从谈情说爱开始
  11. kafka日志清理策略
  12. c#数据格式化之DataFormatString
  13. 关于名为民间借贷实为诈骗案件的讨论
  14. mapinfo二次开发之:MapX和MapXtreme区别
  15. 笔记本固态硬盘温度测试软件,固态硬盘散热测试
  16. 可以下载查看国家自然科学基金的申请文本
  17. 封装bootstrap-treegrid组件
  18. teradata数据库分析函数_TERADATA中函数的使用
  19. 我见过最全的剖析QEMU原理的文章[Z]
  20. 微信企业号_智能机器人_python3

热门文章

  1. eclipse插件SonarLint点击deactivate rule后恢复
  2. 《结构思考力》如何把200ml的水倒入100ml的杯子里?
  3. 学计算机有那些方向,计算机专业的研究生研究方向有哪些
  4. 三方支付之支付宝支付实现逻辑
  5. 上载人生(数字天堂)
  6. LSDSLAM算法解析
  7. ESP32学习笔记(29)——BLE iBeacon广播
  8. 云原生中间件RocketMQ-消费者消费模式之广播模式、偏移量offset解析
  9. 除了汽车,自动驾驶还将颠覆这33个行业……
  10. 名家名言 Chuck Thacker