Windows Phone 8.1 磁贴

network | | 访问(50)
<StackPanel>
    <Button x:Name="tile" Content="创建一个磁贴" Click="tile_Click" Width="370"></Button>
    <Button x:Name="update" Content="更新磁贴" Click="update_Click" Width="370"></Button>
    <Button x:Name="notified" Content="发送磁贴通知" Click="notified_Click" Width="370"></Button>
    <Button x:Name="getTile" Content="获取磁贴" Click="getTile_Click" Width="370"></Button>
    <TextBlock x:Name="info" TextWrapping="Wrap"></TextBlock>
</StackPanel>
public sealed partial class MainPage: Page {
    public MainPage() {
        this.InitializeComponent();

        this.NavigationCacheMode = NavigationCacheMode.Required;
    }
    private string tileId = "001";

    protected override void OnNavigatedTo(NavigationEventArgs e) {
        if (SecondaryTile.Exists(tileId)) {
            tile.Content = "取消磁贴";
        } else {
            tile.Content = "创建一个磁贴";
        }
        if (e.Parameter != null) {
            info.Text = e.Parameter.ToString();
        }
    }

    private async void tile_Click(object sender, RoutedEventArgs e) {
        if (SecondaryTile.Exists(tileId)) {
            SecondaryTile secondaryTile = new SecondaryTile(tileId);
            bool isUnpinned = await secondaryTile.RequestDeleteAsync();
            if (isUnpinned) {
                tile.Content = "创建一个磁贴";
                await new MessageDialog("取消成功").ShowAsync();
            }
        } else {
            Uri square71x71Logo = new Uri("ms-appx:///Assets/Square71x71Logo.scale-240.update.png");
            Uri square150x150Logo = new Uri("ms-appx:///Assets/Logo.scale-240.update.png");
            Uri wide310x150Logo = new Uri("ms-appx:///Assets/WideLogo.scale-240.update.png");
            string tileActivationArguments = "tileId" + " WasPinnedAt=" + DateTime.Now.ToLocalTime().ToString();
            SecondaryTile secondaryTile = new SecondaryTile(tileId, "my tile", tileActivationArguments, square150x150Logo, TileSize.Square150x150);
            secondaryTile.VisualElements.Wide310x150Logo = wide310x150Logo;
            secondaryTile.VisualElements.Square150x150Logo = square150x150Logo;
            secondaryTile.VisualElements.Square71x71Logo = square71x71Logo;
            secondaryTile.VisualElements.ShowNameOnSquare150x150Logo = false;
            bool isPinned = await secondaryTile.RequestCreateAsync();
            if (isPinned) {
                tile.Content = "取消磁贴";
            }
        }

    }

    private async void update_Click(object sender, RoutedEventArgs e) {
        IReadOnlyList < SecondaryTile > tileList = await SecondaryTile.FindAllAsync();
        foreach(SecondaryTile tile in tileList) {
            SecondaryTile secondaryTile = new SecondaryTile(tile.TileId);
            secondaryTile.Arguments = "Updated Arguments(TileId: " + tile.TileId + ")";
            secondaryTile.VisualElements.Square71x71Logo = new Uri("ms-appx:///Assets/Square71x71Logo.scale-240.png");
            secondaryTile.VisualElements.Square150x150Logo = new Uri("ms-appx:///Assets/Logo.scale-240.png");
            secondaryTile.VisualElements.Wide310x150Logo = new Uri("ms-appx:///Assets/WideLogo.scale-240.png");
            bool success = await secondaryTile.UpdateAsync();
            await new MessageDialog(tile.TileId + "更新成功").ShowAsync();
        }
    }

    private async void notified_Click(object sender, RoutedEventArgs e) {
        XmlDocument wideTileXml = TileUpdateManager.GetTemplateContent(TileTemplateType.TileWide310x150IconWithBadgeAndText);
        XmlNodeList wideTileTextAttributes = wideTileXml.GetElementsByTagName("text");
        wideTileTextAttributes[0].AppendChild(wideTileXml.CreateTextNode("hello"));
        wideTileTextAttributes[1].AppendChild(wideTileXml.CreateTextNode("Windows Phone 8.1"));
        wideTileTextAttributes[2].AppendChild(wideTileXml.CreateTextNode("Iam coming"));
        XmlNodeList wideTileImageAttributes = wideTileXml.GetElementsByTagName("image"); ((XmlElement) wideTileImageAttributes[0]).SetAttribute("src", "ms-appx:///Assets/WideLogo.scale-240.png");
        TileNotification wideTileNotification = new TileNotification(wideTileXml);
        wideTileNotification.ExpirationTime = DateTimeOffset.UtcNow.AddSeconds(30);
        TileUpdateManager.CreateTileUpdaterForApplication(tileId).Update(wideTileNotification);
        await new MessageDialog("更新磁贴通知成功").ShowAsync();
    }

    private async void getTile_Click(object sender, RoutedEventArgs e) {
        IReadOnlyList < SecondaryTile > tileList = await SecondaryTile.FindAllAsync();
        info.Text = "应用程序全部SecondaryTile的tileId列表: ";
        foreach(var tile in tileList) {
            info.Text += tile.TileId + ";";
        }
    }
}