My Live Tileは、Event1、Event2、Event3の3種類のイベントからの時間を追跡しています。イベントが発生してからの時間をライブタイルに表示したいのですが。
以下のコードのようなバックグラウンドタスクで実装しましたが、それが正しい方法であるかどうかを知りたいですか?
私が見たすべての例は、Runが1回呼び出されたものであるため、AddTileNotificationは1回だけ呼び出されます。私はそれをWhile(true)ループにあるように変更しましたが、それについてはよくわかりませんか?!
using System;
using System.Threading.Tasks;
using Windows.ApplicationModel.Background;
using Windows.Data.Xml.Dom;
using Windows.Storage;
using Windows.UI.Notifications;
namespace BackgroundTasks
{
public sealed class TileUpdaterClass : IBackgroundTask
{
TileUpdater tileUpdater;
public void Run( IBackgroundTaskInstance taskInstance )
{
while ( true )
{
BackgroundTaskDeferral deferral = taskInstance.GetDeferral();
tileUpdater = TileUpdateManager.CreateTileUpdaterForApplication();
tileUpdater.EnableNotificationQueue( true );
AddTileNotification( "Since Event1: " + TimeSince( "Event1" ), "tag1", "ms-appx:///Resources/Images/Event1.jpg" );
AddTileNotification( "Since Event2: " + TimeSince( "Event2" ), "tag2", "ms-appx:///Resources/Images/Event2.jpg" );
AddTileNotification( "Since Event3: " + TimeSince( "Event3" ), "tag3", "ms-appx:///Resources/Images/Event3.jpg" );
deferral.Complete();
Task.Delay( 5000 );
}
}
private static string TimeSince( string key )
{
TimeSpan since;
if ( ApplicationData.Current.LocalSettings.Values.ContainsKey( key ) )
{
since = new TimeSpan( DateTime.Now.Ticks - ( new DateTime( (long)ApplicationData.Current.LocalSettings.Values[ key ] ) ).Ticks );
}
return ( since.Hours.ToString( "D2" ) + ":" + since.Minutes.ToString( "D2" ) + ":" + since.Seconds.ToString( "D2" ) );
}
private void AddTileNotification( string content, string tag, string image )
{
var templateType = TileTemplateType.TileWideSmallImageAndText04;
var xml = TileUpdateManager.GetTemplateContent( templateType );
var textNodes = xml.GetElementsByTagName( "text" );
textNodes[ 0 ].AppendChild( xml.CreateTextNode( "myApp" ) );
textNodes[ 1 ].AppendChild( xml.CreateTextNode( content ) );
var imageNodes = xml.GetElementsByTagName( "image" );
var elt = (XmlElement)imageNodes[ 0 ];
elt.SetAttribute( "src", image );
var tile = new TileNotification( xml );
tile.Tag = tag;
tileUpdater.Update( tile );
}
}
}
また、タイルに3つのイベントが順番に表示されていないことに気付きました。時間は正しく保持されますが、順序は保持されません。正常ですか?
ありがとう、EitanB