C# 中的 Metro Tile 通知

发布于 2024-12-05 04:59:17 字数 309 浏览 16 评论 0原文

我正在尝试用 C# 将一个简单的 Windows 8 Metro 风格应用程序与磁贴通知组合在一起,但我似乎无法让它们工作。

我还不太清楚更新磁贴通知的代码应该驻留在哪里。我查看了 Javascript 示例,但我没有看到它在 C# 应用程序中如何工作。有没有人获得一些示例代码或有关 C# Metro 应用程序中应在何处进行图块更新的快速提示?

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.

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. Has anyone got some sample code or a quick tip on where tile updates should happen in a C# metro app?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

枯寂 2024-12-12 04:59:17

我的理解是,每个应用程序都会自行决定在哪里执行此操作。通常,只要您还使用相同的数据更新正常的 UI,您就会执行此操作 - 例如,如果您的应用程序是 RSS 阅读器,并且您刚刚下载了要显示的新项目,那么您还可以通过以下方式更新磁贴:发布通知。在示例 JavaScript 应用程序中,为了方便起见,这是通过控件的事件处理程序完成的。

至于更改磁贴的代码,它应该与 JavaScript 版本几乎相同,因为在这两种情况下您都使用 Windows.UI.Notifications 命名空间。以下是一个非常简单的 C# 应用程序,当您单击按钮时,它会更新磁贴。 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>

和背后的代码:

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));
        }
    }
}

不要忘记您需要一个宽磁贴来显示文本 - 要获得它,请在 Package.appxmanifest 中为“宽徽标”设置一些图像。

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 - e.g. 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. In the sample JavaScript app, this is done from event handlers for controls for the sake of convenience.

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. Following is a very simple C# app that updates the tile when you click the button. 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.

一向肩并 2024-12-12 04:59:17

确保将初始旋转更改为横向,为 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("text");
            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);
        }

这是详细说明http://www.amazedsaint。 com/2011/09/hellotiles-simple-c-xaml-application.html

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.

 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("text");
            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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文