首页 > 代码库 > Windows Phone 8.1 Tiles, Notifications and Action Center

Windows Phone 8.1 Tiles, Notifications and Action Center

(1)Tiles

Tiles 也就是磁贴,是 Windows Phone 的一大特色。

一个 Tile 其实可以看成是一个 XML,比如:

<tile>
  <visual>
    <binding template="TileSquareImage">
      <image id="1" src="image1" alt="alt text"/>
    </binding>  
  </visual>
</tile>

<tile>
  <visual version="2">
    <binding template="TileSquare150x150Image" fallback="TileSquareImage">
      <image id="1" src="image1" alt="alt text"/>
    </binding>  
  </visual>
</tile>

微软为我们提供了一系列模板,具体可参照:链接

只要根据模板的 XML 格式,便可轻松的更新 Tile:

private void updateButton_Click(object sender, RoutedEventArgs e)
{
    UpdateTiles("ms-appx:///Images/Middle.png", "ms-appx:///Images/Wide.png");
}

private void UpdateTiles(string middlePath, string widePath)
{
    string tileString = "<tile>" +
                           "<visual version=\"2\">" +
                              "<binding template=\"TileSquare150x150PeekImageAndText04\" fallback=\"TileSquarePeekImageAndText04\">" +
                                  "<image id=\"1\" src=http://www.mamicode.com/"" + middlePath + "\" alt=\"alt text\"/>" +
                                  "<text id=\"1\"></text>" +
                                "</binding>" +
                                "<binding template=\"TileWide310x150ImageAndText01\" fallback=\"TileWideImageAndText01\">" +
                                  "<image id=\"1\" src=http://www.mamicode.com/"" + widePath + "\" alt=\"alt text\"/>" +
                                  "<text id=\"1\"></text>" +
                              "</binding>" +
                           "</visual>" +
                       "</tile>";
    XmlDocument tileXML = new XmlDocument();
    tileXML.LoadXml(tileString);

    TileNotification newTile = new TileNotification(tileXML);
    TileUpdater updater = TileUpdateManager.CreateTileUpdaterForApplication();
    updater.EnableNotificationQueue(false);
    updater.Update(newTile);
}

除了主磁贴外我们还可以新建 SecondaryTile:

private void createButton_Click(object sender, RoutedEventArgs e)
{
    CreateTile("ms-appx:///Images/Middle.png", "ms-appx:///Images/Wide.png");
}

private async void CreateTile(string middlePath, string widePath)
{
    SecondaryTile tile = new SecondaryTile("Cortana", "Cortana", "Some", new Uri(middlePath), TileSize.Default);
    tile.VisualElements.ShowNameOnSquare150x150Logo = true;
    tile.VisualElements.ForegroundText = ForegroundText.Dark;
    tile.VisualElements.Square30x30Logo = new Uri(middlePath);
    tile.VisualElements.Wide310x150Logo = new Uri(widePath);
    await tile.RequestCreateAsync();
}

SecondaryTile 的更新与主磁贴更新一样:

TileUpdater update = TileUpdateManager.CreateTileUpdaterForSecondaryTile("Cortana");

 

(2)Notifications

Notification(推送通知)分为 Tile,Badge,Toast,Raw 四种类型,而通知的方式又分为 Scheduled,Periodic,Local,Push 四种,它们之间对应的关系为:

使用方法都大同小异,根据各自的 XML 格式修改再调用 Update 方法即可,例如:

XmlDocument xml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText04);
ToastNotification toast = new ToastNotification(xml);
ToastNotifier notifier = ToastNotificationManager.CreateToastNotifier();
notifier.Show(toast);

需要注意的是:(1)Toast 通知需要在 Manifest 中许可;(2)Push 方法为:

private async void SendRawNotification()
{
    var channel = await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync();
    channel.PushNotificationReceived += channel_PushNotificationReceived;
}

private void channel_PushNotificationReceived(PushNotificationChannel sender, PushNotificationReceivedEventArgs args)
{
    var raw = args.RawNotification;
}

 

(3)Action Center

1)每个应用最多可以在 Action Center 中驻留 20 条通知

2)通知最多可驻留 7 天

3)可发送静默通知(不会提示用户)

toast1.SuppressPopup = true;

4)可对通知进行分组

XmlDocument xml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText04);

ToastNotification toast1 = new ToastNotification(xml);
toast1.Group = "One";
toast1.Tag = "1";

5)可更新或删除通知(可删除某一组)

ToastNotificationManager.History.RemoveGroup("One");