2011-09-18 20 views
13

C#で単純なWindows 8メトロスタイルのアプリケーションをタイル通知でまとめようとしていますが、動作させることができません。C#のメトロタイル通知

まだわからないのは、タイル通知を更新するコードが存在する場所です。私はJavascript sampleを見てきましたが、C#アプリケーションでどのように動作しているのか分かりません。誰かが、C#メトロアプリでタイルの更新がどこに起こるべきかについて、サンプルコードやクイックヒントを手に入れましたか?

答えて

15

私の理解では、すべてのアプリがそれをどこで行うのかを決めることです。通常、同じデータで通常のUIを更新しているときはいつでもそうします。あなたのアプリがRSSリーダーで、表示する新しいアイテムをダウンロードしたばかりの場合は、通知を送信してタイルを更新します。 JavaScriptアプリケーションのサンプルでは、​​これは、便宜上、コントロールのイベントハンドラから行われます。

タイルを変更するコードは、どちらの場合もWindows.UI.Notifications namespaceを使用するため、JavaScriptバージョンとほとんど同じです。以下は、ボタンをクリックするとタイルを更新する非常にシンプルな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)); 
     } 
    } 
} 

は、あなたがテキストを表示するための広いタイル必要があることを忘れてはいけない - それを得るために、パッケージに「ワイドロゴ」のためのいくつかの画像を設定します.appxmanifest。

+0

ああです!私のタイルはまだ正方形でした。それを広いものに変更し、それは働いた。 –

1

初期回転を横向きに変更し、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); 
     } 

は、ここで詳細な説明http://www.amazedsaint.com/2011/09/hellotiles-simple-c-xaml-application.html