下面将介绍两种方式如何在windows10 uwp开发中如何更新应用磁贴:html
实际上windows的磁贴就是用xml实现的,你只须要建立相应格式的xml就能够实现动态磁贴了windows
一,手动更新磁贴服务器
二,轮询更新磁贴网络
【第一种方式】手动更新磁贴app
private void changeBtn_Click(object sender, RoutedEventArgs e) { //获取模板 var tileXml = TileUpdateManager.GetTemplateContent(TileTemplateType.TileSquare150x150Text01); //获取标签名为text的元素值 var tileAttributes = tileXml.GetElementsByTagName("text"); //将获取的元素值追加一个值 tileAttributes[0].AppendChild(tileXml.CreateTextNode(titleBox.Text)); //建立一个磁贴类,将xml数据填充到磁贴中 var tileNotification = new TileNotification(tileXml); //向磁贴更新 TileUpdateManager.CreateTileUpdaterForApplication().Update(tileNotification); }
这样,当点击按钮时,应用磁贴就会显示文本框输入的值布局
【第二种方式】轮询更新磁贴spa
若是你不了解MVC你能够跳过此步骤,而后看下面如何经过网络获取数据code
并在控制器的Index默认方法里添加如下数据:xml
public ActionResult Index() { ViewBag.Title = "巴黎发生恐袭"; ViewBag.Content = "数百人遇难"; ViewBag.Type = "新闻"; return View(); }
而后再Hello视图中添加Index视图,把布局页模板页什么的勾选去掉,而后新建了一个.cshtml文件,最后你要把Index.cshtml中的全部html数据所有清空替换成如下文档结构htm
<tile> <visual version="2"> <binding template="TileSquare150x150Text01" fallback="TileSquareText01"> <text id="1">@ViewBag.Title</text> <text id="2">@ViewBag.Content</text> <text id="3">@ViewBag.Type</text> <text id="4">@DateTime.Now.ToLongTimeString()</text> </binding> </visual> </tile>
待会再介绍模板问题
最后一步,回到UWP项目中,建立一个按钮(当按下这个按钮时,之后磁贴会按照你设定时间进行自动轮询更新),添加一个单击事件事件,添加如下代码:
private void AutoUpdate_Click(object sender, RoutedEventArgs e) { //设置一个Uri类型变量保存服务器的xml地址 var tileContent = new Uri("http://localhost:61341/UWP/Index"); //设置轮询时间变量为半小时,也能够设置其余时间 var requestedInterval = PeriodicUpdateRecurrence.HalfHour; //建立磁贴更新实例 var updater = TileUpdateManager.CreateTileUpdaterForApplication(); //开始轮询更新,传入服务器磁贴xml文件地址和轮询时间 updater.StartPeriodicUpdate(tileContent, requestedInterval); }
这样一来你的应用就实现了自动磁贴更新的功能。
下面咱们再来讨论一下磁贴模板问题: