导出 (0) 打印
全部展开
信息
您所需的主题如下所示。但此主题未包含在此库中。

如何创建、删除和更新 Windows Phone OS 7.1 的磁贴

2014/6/18

适用于:Windows Phone OS 7.1

本示例演示如何使用 ShellTile API 更新应用磁贴。还演示如何创建、删除和更新次要磁贴。

说明注意:

如果您正在查找有关如何创造和更新 Windows Phone 8 磁贴的信息, 请参见相应的磁贴模板主题:Windows Phone 8 翻转磁贴模板Windows Phone 8 循环磁贴模板Windows Phone 8 图标磁贴模板

本示例应用包含三个页面。MainPage 只是允许导航到其他两个页面。ApplicationTile 页面允许您更新应用磁贴的属性。一次设置所有属性。SecondaryTile 页面允许您创建、更新和删除次要磁贴。该页面允许您单独设置每个属性。还演示从 QueryString 获取值。如果用户从 MainPage 导航到 SecondaryTile 页面,则 DefaultTitle 值将为 FromMain。如果用户通过点按次要磁贴导航到 SecondaryTile 页面,则 DefaultTitle 值将为 FromTile

说明注意:

请注意,ShellTile 可用于更新应用磁贴的属性,但不能用于创建或删除应用磁贴。

您可以从磁贴示例下载完整的示例。

在此步骤中,您创建一个新的项目并为应用中的以下三个页面设计 UI:MainPage、ApplicationTile 和 SecondaryTile。

创建 UI 布局

  1. 创建一个新的 Windows Phone 应用,并将其命名为 TileSample

  2. 使用 Microsoft 绘图或其他编辑图像的工具创建三个将用作背景图像的磁贴大小的图像文件。为了简单起见,您可以使这三个图像成为纯色图像,即红色、蓝色和绿色。将它们命名为 Red.jpg、Blue.jpg 和 Green.jpg。您也可以下载磁贴示例 并使用其中包含的图像。在“解决方案资源管理器”中选择 TileSample 项目,然后从上下文菜单中选择“项目” | “添加现有项”,将这三个图像文件添加到您的项目中。选择这三个图像文件,然后单击“添加”。对于每个图像文件,将“生成操作”属性设置为“内容”

  3. 通过将 TitlePanel 替换为以下 XAML 代码来设置应用的名称以及 MainPage 中的页面标题。

            <!--TitlePanel contains the name of the app and page title-->
            <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
                <TextBlock x:Name="ApplicationTitle" Text="Tile Sample" Style="{StaticResource PhoneTextNormalStyle}"/>
                <TextBlock x:Name="PageTitle" Text="Tiles" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
            </StackPanel>
    
    
  4. MainPage 的 XAML 代码如下。该代码包含一个文本块和两个按钮。

    控件类型

    控件名称

    控件内容

    TextBlock

    textBlockDescription

    本示例演示如何更新应用磁贴,还演示如何创建、更新和删除次要磁贴。

    按钮

    buttonChangeApplicationTile

    更改应用磁贴。

    按钮

    buttonChangeSecondaryTile

    更改次要磁贴。

    每个按钮都将处理 button_Click 事件。

    若要创建 MainPage UI,请用以下代码替换 MainPage.xaml 中的 <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"></Grid>

            <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
                <TextBlock Height="170" HorizontalAlignment="Left" Margin="12,6,0,0" Name="textBlockDescription" Text="This sample demonstrates how to update Application Tiles.  It also demonstrates how to create, update, and delete secondary Tiles." VerticalAlignment="Top" Width="438" TextWrapping="Wrap" TextAlignment="Left" />
                <Button Content="Change Application Tile" Height="72" HorizontalAlignment="Left" Margin="28,182,0,0" Name="buttonChangeApplicationTile" VerticalAlignment="Top" Width="392" Click="buttonChangeApplicationTile_Click" />
                <Button Content="Change Secondary Tile" Height="72" HorizontalAlignment="Left" Margin="28,278,0,0" Name="buttonChangeSecondaryTile" VerticalAlignment="Top" Width="392" Click="buttonChangeSecondaryTile_Click" />
            </Grid>
    
    
  5. 下一步是创建 ApplicationTile.xaml 的 UI。若要创建 ApplicationTile UI,请在“解决方案资源管理器”中选择 TileSample 项目名称,然后从菜单中选择“项目” | “添加新项”。添加一个新的“Windows Phone 纵向页面”并将其命名为 Page2.xaml。单击“添加”,此时会创建 ApplicationTile.xaml 和 ApplicationTile.xaml.cs 文件。

  6. 通过将 TitlePanel 替换为以下 XAML 代码来设置应用的名称以及 ApplicationTile 页面中的页面标题。

            <!--TitlePanel contains the name of the app and page title-->
            <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
                <TextBlock x:Name="ApplicationTitle" Text="Tile Sample" Style="{StaticResource PhoneTextNormalStyle}"/>
                <TextBlock x:Name="PageTitle" Text="Application Tile" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}" FontSize="64" />
            </StackPanel>
    
    
  7. ApplicationTile 页面的 XAML 代码如下。该代码包含一个文本块、六个文本块、六个文本框以及一个用于设置属性的按钮。

    控件类型

    控件名称

    控件内容

    TextBlock

    textBlockDescription

    以组的形式设置应用磁贴的属性。将文本和图像属性保留为空以清除属性。将“计数”设置为 0 以清除“计数”属性。

    TextBlock

    textBlockTitle

    标题

    TextBox

    textBoxTitle

    <blank>

    TextBlock

    textBlockBackgroundImage

    背景图像

    TextBox

    textBoxBackground

    <blank>

    TextBlock

    textBlockCount

    计数(1-99,0=清除)

    TextBox

    textBoxCount

    <blank>

    TextBlock

    textBlockBackTitle

    BackTitle

    TextBox

    textBoxBackTitle

    <blank>

    TextBlock

    textBlockBackBackgroundImage

    背面背景图像

    TextBox

    textBoxBackBackGround

    <blank>

    TextBlock

    textBlockBackContent

    BackContent

    TextBox

    textBoxBackContent

    <blank>

    按钮

    buttonSetApplicationTile

    设置应用磁贴属性

    buttonSetApplicationTile 将处理 button_Click 事件。

    将 ApplicationTile.xaml 中的 <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"></Grid> 替换为以下代码。

            <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
                <TextBlock Height="75" HorizontalAlignment="Left" Margin="12,6,0,0" Name="textBlockDescription" Text="Sets Application Tile properties as a group. Leave text and image properties blank to clear the property. Set Count to 0 to clear the Count property." VerticalAlignment="Top" Width="438" TextWrapping="Wrap" FontSize="16" />
                <TextBlock Height="30" HorizontalAlignment="Left" Margin="164,96,0,0" Name="textBlockTitle" Text="Title" VerticalAlignment="Top" FontSize="18" />
                <TextBox Height="72" HorizontalAlignment="Left" Margin="197,74,0,0" Name="textBoxTitle" Text="" VerticalAlignment="Top" Width="260" />
                <TextBlock Height="30" HorizontalAlignment="Left" Margin="46,171,0,0" Name="textBlockBackgroundImage" Text="Background Image" VerticalAlignment="Top" FontSize="18" />
                <TextBox Height="72" HorizontalAlignment="Left" Margin="197,152,0,0" Name="textBoxBackgroundImage" Text="" VerticalAlignment="Top" Width="260" />
                <TextBlock Height="30" HorizontalAlignment="Left" Margin="23,249,0,0" Name="textBlockCount" Text="Count (1-99, 0=Clear)" VerticalAlignment="Top" FontSize="18" Width="175" />
                <TextBox Height="72" HorizontalAlignment="Left" Margin="197,230,0,0" Name="textBoxCount" Text="" VerticalAlignment="Top" Width="260" InputScope="Number" />
                <TextBlock Height="30" HorizontalAlignment="Left" Margin="127,359,0,0" Name="textBlockBackTitle" Text="BackTitle" VerticalAlignment="Top" FontSize="18" />
                <TextBox Height="72" HorizontalAlignment="Left" Margin="197,338,0,0" Name="textBoxBackTitle" Text="" VerticalAlignment="Top" Width="260" />
                <TextBlock Height="30" HorizontalAlignment="Left" Margin="10,436,0,0" Name="textBlockBackBackgroundImage" Text="BackBackground Image" VerticalAlignment="Top" FontSize="18" />
                <TextBox Height="72" HorizontalAlignment="Left" Margin="197,416,0,0" Name="textBoxBackBackgroundImage" Text="" VerticalAlignment="Top" Width="260" />
                <TextBlock Height="30" HorizontalAlignment="Left" Margin="97,515,0,0" Name="textBlockContent" Text="BackContent" VerticalAlignment="Top" FontSize="18" />
                <TextBox Height="72" HorizontalAlignment="Left" Margin="197,494,0,0" Name="textBoxBackContent" Text="" VerticalAlignment="Top" Width="260" />
                <Button Content="Set Application Tile Properties" Height="72" HorizontalAlignment="Left" Margin="23,551,23,0" Name="buttonSetApplicationTile" VerticalAlignment="Top" Width="410" FontSize="20" FontFamily="Calibri" Click="buttonSetApplicationTile_Click" />
            </Grid>
    
    
  8. 创建 UI 的最后一步是添加 SecondaryTile.xaml 页面。若要创建 SecondaryTile UI,请在“解决方案资源浏览器”中选择 TileSample 项目名称,然后从菜单中选择“项目” | “添加新项”。添加一个新的“Windows Phone 纵向页面”并将其命名为 Page2.xaml。单击“添加”,此时会创建 SecondaryTile.xaml 和 SecondaryTile.xaml.cs 文件。

  9. 通过将 TitlePanel 替换为以下 XAML 代码来设置应用的名称以及 SecondaryTile 页面中的页面标题。

            <!--TitlePanel contains the name of the app and page title-->
            <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
                <TextBlock x:Name="ApplicationTitle" Text="Tile Sample" Style="{StaticResource PhoneTextNormalStyle}"/>
                <TextBlock x:Name="PageTitle" Text="Secondary Tile" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}" FontSize="64" />
            </StackPanel>
    
    
  10. SecondaryTile 页面的 XAML 代码如下。该代码包含一个复选框、一个文本块、六个文本框和六个按钮。

    控件类型

    控件名称

    控件内容

    复选框

    checkBoxDisplaySecondaryTile

    显示次要磁贴

    TextBlock

    textBlockDescription

    单独设置次要磁贴的属性。

    TextBox

    textBoxTitle

    <blank>

    按钮

    buttonTitle

    标题

    TextBox

    textBoxBackGround

    <blank>

    按钮

    buttonBackGround

    背景

    TextBox

    textBoxCount

    <blank>

    按钮

    buttonCount

    计数

    TextBox

    textBoxBackTitle

    <blank>

    按钮

    buttonBackTitle

    背面标题

    TextBox

    textBoxBackBackGround

    <blank>

    按钮

    buttonBackBackGround

    背面背景

    TextBox

    textBoxBackContent

    <blank>

    按钮

    buttonBackContent

    背面内容

    checkBoxDisplaySecondaryTile 控件将处理 checkboxDisplaySecondaryTile_CheckedcheckboxDisplaySecondaryTile_Unchecked 事件。

    每个按钮都将处理 button_Click 事件。

    将 Secondary.xaml 中的 <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"></Grid> 替换为以下代码。

            <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
                <CheckBox Content="Display Secondary Tile " Height="72" HorizontalAlignment="Left" Margin="20,4,0,0" Name="checkBoxDisplaySecondaryTile" VerticalAlignment="Top" Checked="checkBoxDisplaySecondaryTile_Checked" Unchecked="checkBoxDisplaySecondaryTile_Unchecked" />
                <TextBlock Height="33" HorizontalAlignment="Center" Margin="9,91,0,0" Name="textBlockDescription" Text="Sets properties of the secondary Tile individually." VerticalAlignment="Top" Width="441" TextAlignment="Center" />
    
                <TextBox Height="72" HorizontalAlignment="Left" Margin="9,127,0,0" Name="textBoxTitle" Text="" VerticalAlignment="Top" Width="260" />
                <Button Content="Set Title" Height="72" HorizontalAlignment="Left" Margin="259,127,0,0" Name="buttonSetTitle" VerticalAlignment="Top" Width="200" Click="buttonSetTitle_Click" FontSize="16" />
    
                <TextBox Height="72" HorizontalAlignment="Left" Margin="9,205,0,0" Name="textBoxBackgroundImage" Text="" VerticalAlignment="Top" Width="260" />
                <Button Content="Set Background" Height="72" HorizontalAlignment="Left" Margin="259,205,0,0" Name="buttonSetBackgroundImage" VerticalAlignment="Top" Width="200" FontSize="16" Click="buttonSetBackgroundImage_Click" />
    
                <TextBox Height="72" HorizontalAlignment="Left" Margin="9,283,0,0" Name="textBoxCount" Text="" VerticalAlignment="Top" Width="260"  InputScope="Number"/>
                <Button Content="Set Count(1-99,0=Clear)" Height="72" HorizontalAlignment="Left" Margin="259,283,0,0" Name="buttonSetCount" VerticalAlignment="Top" Width="200" Click="buttonSetCount_Click" FontSize="14" />
    
                <TextBox Height="72" HorizontalAlignment="Left" Margin="9,391,0,0" Name="textBoxBackTitle" Text="" VerticalAlignment="Top" Width="260" />
                <Button Content="Set BackTitle" Height="72" HorizontalAlignment="Left" Margin="259,391,0,0" Name="buttonSetBackTitle" VerticalAlignment="Top" Width="200" Click="buttonSetBackTitle_Click" FontSize="16" />
    
                <TextBox Height="72" HorizontalAlignment="Left" Margin="9,469,0,0" Name="textBoxBackBackgroundImage" Text="" VerticalAlignment="Top" Width="260" />
                <Button Content="Set BackBackground" Height="72" HorizontalAlignment="Left" Margin="259,469,0,0" Name="buttonSetBackBackgroundImage" VerticalAlignment="Top" Width="200" FontSize="16" Click="buttonSetBackBackgroundImage_Click" />
    
                <TextBox Height="72" HorizontalAlignment="Left" Margin="9,547,0,0" Name="textBoxBackContent" Text="" VerticalAlignment="Top" Width="260" />
                <Button Content="Set BackContent" Height="72" HorizontalAlignment="Left" Margin="259,547,0,0" Name="buttonSetBackContent" VerticalAlignment="Top" Width="200" FontSize="16" Click="buttonSetBackContent_Click" />
            </Grid>
    
    
    

实现 MainPage 需要为 MainPage 上的两个按钮添加事件处理程序。

实现 MainPage 功能

  • 在 MainPage.xaml.cs 中添加以下代码:该代码为 button_Click 事件实现事件处理程序。单击某个按钮时,应用会导航到相应的页面。当导航到 SecondaryTile 页面时,参数 DefaultTitle 设置为 FromMain,以便 SecondaryTile 页面知道导航来自哪里。 之后,当从某个磁贴导航到 SecondaryPage 时,我们将 DefaultTitle 设置为 FromTile

    
            // Navigate to the page for modifying Application Tile properties.
            private void buttonChangeApplicationTile_Click(object sender, RoutedEventArgs e)
            {
                this.NavigationService.Navigate(new Uri("/ApplicationTile.xaml", UriKind.Relative));
            }
    
    
            // Navigate to the page for modifying secondary Tile properties. 
            // Pass a parameter that lets the SecondaryTile page know that it was navigated to from MainPage.
            // (DefaultTitle will equal 'FromTile' when the user navigates to the SecondaryTile page from a Tile.
            private void buttonChangeSecondaryTile_Click(object sender, RoutedEventArgs e)
            {
                this.NavigationService.Navigate(new Uri("/SecondaryTile.xaml?DefaultTitle=FromMain", UriKind.Relative));
            }
    
    

实现 ApplicationTile 页面将需要添加 buttonSetApplicationTile_Click 事件的事件处理程序。

实现 ApplicationTile 页面功能

  1. 对于 ShellTile 命名空间,向 ApplicationTile.xaml.cs 文件的顶部添加一个 using 指令。

    using Microsoft.Phone.Shell;
    
    
  2. 在 ApplicationTile.xaml.cs 中添加以下代码:该代码将为 button_Click 事件实现事件处理程序。将获取文本框中输入的值并一次设置应用磁贴的所有属性。

    
            // Set all the properties of the Application Tile.
            private void buttonSetApplicationTile_Click(object sender, RoutedEventArgs e)
            {
                int newCount = 0;
    
                // Application Tile is always the first Tile, even if it is not pinned to Start.
                ShellTile TileToFind = ShellTile.ActiveTiles.First();
    
                // Application should always be found
                if (TileToFind != null)
                {
                    // if Count was not entered, then assume a value of 0
                    if (textBoxCount.Text == "")
                    {
                        // A value of '0' means do not display the Count.
                        newCount = 0;
                    }
                    // otherwise, get the numerical value for Count
                    else
                    {
                        newCount = int.Parse(textBoxCount.Text);
                    }
    
                    // Set the properties to update for the Application Tile.
                    // Empty strings for the text values and URIs will result in the property being cleared.
                    StandardTileData NewTileData = new StandardTileData
                    {
                        Title = textBoxTitle.Text,
                        BackgroundImage = new Uri(textBoxBackgroundImage.Text, UriKind.Relative),
                        Count = newCount,
                        BackTitle = textBoxBackTitle.Text,
                        BackBackgroundImage = new Uri(textBoxBackBackgroundImage.Text, UriKind.Relative),
                        BackContent = textBoxBackContent.Text
                    };
    
                    // Update the Application Tile
                    TileToFind.Update(NewTileData);
                }
            }
    
    

实现 SecondaryTile 页面需要根据次要磁贴的状态设置复选框,根据是否选中复选框创建或删除复选框以及为某个按钮添加事件处理程序。

实现 SecondaryTile 页面功能

  1. 对于 ShellTile 命名空间,向 SecondaryTile.xaml.cs 文件的顶部添加一个 using 指令。

    using Microsoft.Phone.Shell;
    
    
  2. 添加 OnNavigatedTo 事件处理程序。当我们导航到该页面时,我们需要查看次要磁贴是否存在并相应地设置复选框。此处还根据 QueryString 中设置的 DefaultTitle,设置磁贴的默认值。

    
            // Event handler for when this page is navigated to. Looks to see
            // whether the Tile exists and set the check box appropriately.
            // Also fills in the default value for the Title, based on the
            // value passed in in the QueryString - either FromMain or FromTile.        
            protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
            {
                base.OnNavigatedTo(e);
    
                // See whether the Tile is pinned, and if so, make sure the check box for it is checked.
                // (User may have deleted it manually.)
                ShellTile TileToFind = ShellTile.ActiveTiles.FirstOrDefault(x => x.NavigationUri.ToString().Contains("DefaultTitle=FromTile"));
    
                checkBoxDisplaySecondaryTile.IsChecked = (TileToFind != null);
    
                // To demonstrate the use of the Navigation URI and query parameters, we set the default value
                // of the Title text box based on where we navigated from. If we navigated to this page
                // from the MainPage, the DefaultTitle parameter will be "FromMain". If we navigated here
                // when the secondary Tile was tapped, the parameter will be "FromTile".
                textBoxTitle.Text = this.NavigationContext.QueryString["DefaultTitle"];
    
            }
    
    
  3. checkBoxDisplaySecondaryTile_Checked 事件处理程序添加以下代码。该代码查看是否存在具有我们指定的 ID 的磁贴。如果存在这样的磁贴,则该代码创建一个 StandardTileData 对象,并使用该对象设置该磁贴正面和背面的属性。

    
            // Event handler for when the check box is checked. Create a secondary Tile if it doesn't
            // already exist.
            private void checkBoxDisplaySecondaryTile_Checked(object sender, RoutedEventArgs e)
            {
                // Look to see whether the Tile already exists and if so, don't try to create it again.
                ShellTile TileToFind = ShellTile.ActiveTiles.FirstOrDefault(x => x.NavigationUri.ToString().Contains("DefaultTitle=FromTile"));
    
                // Create the Tile if we didn't find that it already exists.
                if (TileToFind == null)
                {
                    // Create the Tile object and set some initial properties for the Tile.
                    // The Count value of 12 shows the number 12 on the front of the Tile. Valid values are 1-99.
                    // A Count value of 0 indicates that the Count should not be displayed.
                    StandardTileData NewTileData = new StandardTileData
                    {
                        BackgroundImage = new Uri("Red.jpg", UriKind.Relative),
                        Title = "Secondary Tile",
                        Count = 12,
                        BackTitle = "Back of Tile",
                        BackContent = "Welcome to the back of the Tile",
                        BackBackgroundImage = new Uri("Blue.jpg", UriKind.Relative)
                    };
    
                    // Create the Tile and pin it to Start. This will cause a navigation to Start and a deactivation of our app.
                    ShellTile.Create(new Uri("/SecondaryTile.xaml?DefaultTitle=FromTile", UriKind.Relative), NewTileData);
                }
    
            }
    
    
    
  4. checkBoxDisplaySecondaryTile_Unchecked 事件处理程序添加以下代码。该代码搜索具有我们指定的 ID 的磁贴,如果找到,则将它从“开始”屏幕中删除。

    
            // Event handler for when the check box is unchecked.  Delete the secondary Tile
            // if it exists.
            private void checkBoxDisplaySecondaryTile_Unchecked(object sender, RoutedEventArgs e)
            {
                // Find the Tile we want to delete.
                ShellTile TileToFind = ShellTile.ActiveTiles.FirstOrDefault(x => x.NavigationUri.ToString().Contains("DefaultTitle=FromTile"));
    
                // If the Tile was found, then delete it.
                if (TileToFind != null)
                {
                    TileToFind.Delete();
                }
    
            }
    
    
  5. 添加每个 button_Click 事件处理程序。每个事件处理程序都查找要更新的磁贴,从相应的文本框中获取新值,然后用新值更新磁贴。

    
            // Handle the Title button clicked event by setting the front of Tile title.
            private void buttonSetTitle_Click(object sender, RoutedEventArgs e)
            {
                // Find the Tile we want to update.
                ShellTile TileToFind = ShellTile.ActiveTiles.FirstOrDefault(x => x.NavigationUri.ToString().Contains("DefaultTitle=FromTile"));
    
                // If the Tile was found, then update the Title.
                if (TileToFind != null)
                {
                    StandardTileData NewTileData = new StandardTileData
                    {
                        Title = textBoxTitle.Text
                    };
    
                    TileToFind.Update(NewTileData);
                }
    
            }
    
    
            // Handle the Background Image button clicked event by setting the front of Tile background image.
            private void buttonSetBackgroundImage_Click(object sender, RoutedEventArgs e)
            {
                // Find the Tile we want to update.
                ShellTile TileToFind = ShellTile.ActiveTiles.FirstOrDefault(x => x.NavigationUri.ToString().Contains("DefaultTitle=FromTile"));
    
                // If the Tile was found, then update the background image.
                if (TileToFind != null)
                {
                    StandardTileData NewTileData = new StandardTileData
                    {
                        BackgroundImage = new Uri(textBoxBackgroundImage.Text, UriKind.Relative)
                    };
    
                    TileToFind.Update(NewTileData);
                }
    
            }
    
            // Handle the Count button clicked event by setting the front of Tile count value.
            private void buttonSetCount_Click(object sender, RoutedEventArgs e)
            {
                int newCount = 0;
    
                // Find the Tile we want to update.
                ShellTile TileToFind = ShellTile.ActiveTiles.FirstOrDefault(x => x.NavigationUri.ToString().Contains("DefaultTitle=FromTile"));
    
                // If the Tile was found, then update the count.
                if (TileToFind != null)
                {
                    // if Count was not entered, then assume a value of 0.
                    if (textBoxCount.Text == "")
                    {
                        newCount = 0;
                    }
                    // Otherwise, get the numerical value for Count.
                    else
                    {
                        newCount = int.Parse(textBoxCount.Text);
                    }
    
                    StandardTileData NewTileData = new StandardTileData
                    {
                        Count = newCount
                    };
    
                    TileToFind.Update(NewTileData);
                }
    
            }
    
    
            // Handle the Back Title button clicked event by setting the back of Tile title.
            private void buttonSetBackTitle_Click(object sender, RoutedEventArgs e)
            {
                // Find the Tile we want to update.
                ShellTile TileToFind = ShellTile.ActiveTiles.FirstOrDefault(x => x.NavigationUri.ToString().Contains("DefaultTitle=FromTile"));
    
                // If the Tile was found, then update the title on the back of the Tile.
                if (TileToFind != null)
                {
                    StandardTileData NewTileData = new StandardTileData
                    {
                        BackTitle = textBoxBackTitle.Text
                    };
    
                    TileToFind.Update(NewTileData);
                }
    
            }
    
    
            // Handle the Back Background Image button clicked event by setting the back of Tile background image.
            private void buttonSetBackBackgroundImage_Click(object sender, RoutedEventArgs e)
            {
                // Find the Tile we want to update.
                ShellTile TileToFind = ShellTile.ActiveTiles.FirstOrDefault(x => x.NavigationUri.ToString().Contains("DefaultTitle=FromTile"));
    
                // If the Tile was found, then update the background image on the back of the Tile.
                if (TileToFind != null)
                {
                    StandardTileData NewTileData = new StandardTileData
                    {
                        BackBackgroundImage = new Uri(textBoxBackBackgroundImage.Text, UriKind.Relative)
                    };
    
                    TileToFind.Update(NewTileData);
                }
    
            }
    
            // Handle the Back Content button clicked event by setting the back of Tile content.
            private void buttonSetBackContent_Click(object sender, RoutedEventArgs e)
            {
                // Find the Tile we want to update.
                ShellTile TileToFind = ShellTile.ActiveTiles.FirstOrDefault(x => x.NavigationUri.ToString().Contains("DefaultTitle=FromTile"));
    
                // If the Tile was found, then update the content on the back of the Tile.
                if (TileToFind != null)
                {
                    StandardTileData NewTileData = new StandardTileData
                    {
                        BackContent = textBoxBackContent.Text
                    };
    
                    TileToFind.Update(NewTileData);
                }
    
            }
    
    
    

现在,您已经完成了创建、删除和更新次要磁贴的代码。

测试应用:

  1. 按 F5 开始调试应用。该应用将在模拟器中启动。

  2. 导航到“更改应用磁贴”页面。设置应用磁贴的某些属性,如以下屏幕截图所示,然后单击“设置应用磁贴属性”按钮。

  3. 若要查看您设置的属性的结果,请通过按手机模拟器上的“开始”按钮导航到“开始”。导航到应用列表并在该列表中找到 TileSample。长按 TileSample,然后选择“固定到‘开始’屏幕”。模拟器将导航到“开始”屏幕并添加应用磁贴。该应用磁贴应该具有您在上面步骤中设置的属性。

  4. 按手机模拟器上的“返回”按钮导航回到您的应用。通过按“返回”按钮,您的调试会话将不会结束。

  5. 再次按“返回”按钮以导航到应用的 MainPage。

  6. 导航到 SecondaryTile 页面并选中复选框以创建次要磁贴。手机将自动导航到“开始”屏幕并显示新的磁贴。

  7. 按手机模拟器上的“返回”按钮导航回到您的应用。

  8. “标题”属性输入新值,然后单击“设置标题”按钮。

  9. 通过按手机模拟器上的“开始”按钮导航回到“开始”屏幕。应该显示新的“标题”属性。

  10. 再次按“返回”按钮并尝试更改其他属性。

  11. “开始”屏幕中,尝试点按次要磁贴。TileSample 应用将启动并直接导航到 SecondaryTile 页面。“标题”的默认值将为 FromTile

显示:
© 2014 Microsoft