简体   繁体   English

C#中的Metro Tile通知

[英]Metro Tile Notifications in C#

I'm trying to put together a simple Windows 8 metro style app in c# with tile notifications but I can't seem to get them working. 我正在尝试在c#中使用平铺通知组合一个简单的Windows 8 metro风格的应用程序,但我似乎无法让它们正常工作。

What I can't quite figure out yet is where the code to update the tile notifications should reside. 我还不太清楚的是更新磁贴通知的代码应该驻留在哪里。 I've had a look at the Javascript sample , but I'm not seeing how that works in a C# app. 我已经看过Javascript示例 ,但我没有看到它在C#应用程序中是如何工作的。 Has anyone got some sample code or a quick tip on where tile updates should happen in a C# metro app? 有没有人得到一些示例代码或快速提示在C#metro应用程序中应该发生磁贴更新?

My understanding is that every app decides where to do this for itself. 我的理解是每个应用程序都决定在哪里为自己做这件事。 Normally, you'd do it whenever you're also updating your normal UI with the same data - eg if your app is an RSS reader, and you've just downloaded a new item to display, that's where you also update your tile by posting a notification. 通常情况下,每当您使用相同的数据更新普通用户界面时,您都会这样做 - 例如,如果您的应用程序是RSS阅读器,并且您刚刚下载了要显示的新项目,那么您也可以通过以下方式更新您的磁贴发布通知。 In the sample JavaScript app, this is done from event handlers for controls for the sake of convenience. 在示例JavaScript应用程序中,为方便起见,这是从控件的事件处理程序完成的。

As for the code to change the tile, it should be almost identical to JavaScript version, since in both cases you use Windows.UI.Notifications namespace . 至于更改磁贴的代码,它应该与JavaScript版本几乎相同,因为在这两种情况下都使用Windows.UI.Notifications命名空间 Following is a very simple C# app that updates the tile when you click the button. 以下是一个非常简单的C#应用​​程序,可在您单击按钮时更新磁贴。 XAML: XAML:

<UserControl x:Class="TileNotificationCS.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    d:DesignHeight="768" d:DesignWidth="1366">
    <StackPanel x:Name="LayoutRoot" Background="#FF0C0C0C">
        <TextBox x:Name="message"/>
        <Button x:Name="changeTile" Content="Change Tile" Click="changeTile_Click" />
    </StackPanel>
</UserControl>

and code behind: 和代码背后:

using System;
using Windows.Data.Xml.Dom;
using Windows.UI.Notifications;
using Windows.UI.Xaml;

namespace TileNotificationCS
{
    partial class MainPage
    {
        TileUpdater tileUpdater = TileUpdateManager.CreateTileUpdaterForApplication();

        public MainPage()
        {
            InitializeComponent();
        }

        private void changeTile_Click(object sender, RoutedEventArgs e)
        {
            XmlDocument tileXml = TileUpdateManager.GetTemplateContent(TileTemplateType.TileWideText01);
            XmlElement textElement = (XmlElement)tileXml.GetElementsByTagName("text")[0];
            textElement.AppendChild(tileXml.CreateTextNode(message.Text));
            tileUpdater.Update(new TileNotification(tileXml));
        }
    }
}

Don't forget that you need a wide tile for text to show up - to get it, set some image for "Wide Logo" in Package.appxmanifest. 不要忘记你需要一个宽的图块来显示文本 - 为了得到它,在Package.appxmanifest中为“Wide Logo”设置一些图像。

Make sure you change the Initial rotation to Landscape, set some image for Widelogo, and use this method to set the text along with an expiry. 确保将Initial rotation更改为Landscape,为Widelogo设置一些图像,并使用此方法设置文本以及到期日期。

 void SendTileTextNotification(string text, int secondsExpire)
        {
            // Get a filled in version of the template by using getTemplateContent
            var tileXml = TileUpdateManager.GetTemplateContent(TileTemplateType.TileWideText03);

            // You will need to look at the template documentation to know how many text fields a particular template has       

            // get the text attributes for this template and fill them in
            var tileAttributes = tileXml.GetElementsByTagName(&quot;text&quot;);
            tileAttributes[0].AppendChild(tileXml.CreateTextNode(text));

            // create the notification from the XML
            var tileNotification = new TileNotification(tileXml);

            // send the notification to the app's default tile
            TileUpdateManager.CreateTileUpdaterForApplication().Update(tileNotification);
        }

Here is a detailed explanation http://www.amazedsaint.com/2011/09/hellotiles-simple-c-xaml-application.html 以下是http://www.amazedsaint.com/2011/09/hellotiles-simple-c-xaml-application.html的详细说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM