How to add and remove Secondary Tiles in Windows Phone apps
published on: 12/7/2011 | Views: N/A | Tags: Mango
by WindowsPhoneGeek
I am starting a series of articles that will guide you through the process of implementing Tiles in Windows Phone apps.
The first post is about how to add and remove secondary Tiles.
To begin with let me first explain in short what is a Tile in Windows phone.
A Tile is a link to an application displayed in Start. There are two types of Tiles - Application Tiles and secondary Tiles.
The Application Tile is the Tile created when a user pins an application to Start by pressing and holding the application's icon in the Applications List. Tapping a pinned Application Tile navigates the user to the application's opening page.
A secondary Tile is created programmatically by an application based on interaction from the user. Typical uses for a secondary Tile include scenarios where you want to show important information to the user on the Start screen of the phone.
For reference: MSDN documentation
Add and Remove Secondary Tile Step by Step
Step1. Create a new Windows Phone application project, add a new Image folder and add a sample image inside the folder. In our case logo.png:
Step2. We will add a CheckBox that will be used to Add/Remove secondary Tiles.
<StackPanel x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<CheckBox x:Name="cbShowTile" Content="Show seconadry tile"
Checked="cbShowTile_Checked"
Unchecked="cbShowTile_Unchecked"/>
<TextBlock TextWrapping="Wrap" Text="When a new tile is created, the UI will navigate to Start." />
</StackPanel>
Step3. Add a new GetSecondaryTileData() method that will create a new StandardTileData object that contains the information necessary in order to create a Tile.
private StandardTileData GetSecondaryTileData()
{
StandardTileData tileData = new StandardTileData
{
Title = "Secondary Tile",
BackgroundImage = new Uri("/Images/logo.png", UriKind.Relative),
Count = 5,
BackTitle = "Secondary Tile",
BackBackgroundImage = new Uri("", UriKind.Relative),
BackContent = "WPG Add Remove Tile Sample"
};
return tileData;
}
Step4. Add a new method FindTile(string partOfUri) that finds a particular Tile by checking if its navigation uri contains a given string. This method is used whenever we want to get the instance of a particular Tile(if one already exists).
private ShellTile FindTile(string partOfUri)
{
ShellTile shellTile = ShellTile.ActiveTiles.FirstOrDefault(
tile => tile.NavigationUri.ToString().Contains(partOfUri));
return shellTile;
}
Step5. When the CheckBox is checked a new tile is created, the UI will navigate to Start.
NOTE: A secondary Tile can be created only as the result of user input in an application. The application then uses the Create(Uri, ShellTileData) method to create a Tile on Start. Because the UI will navigate to Start when a new secondary Tile is created, only one secondary Tile can be created at a time.
private static readonly string SecondaryTileUriSource = "Source=SeconaryTile";
private void cbShowTile_Checked(object sender, RoutedEventArgs e)
{
// secondary tiles can be created only as the result
// of user input in an application
ShellTile tile = this.FindTile(SecondaryTileUriSource);
if (tile == null)
{
// because the UI will navigate to Start
// when a new secondary tile is created
// only one secondary tile can be created at a time
StandardTileData tileData = this.GetSecondaryTileData();
// having a unique NavigationUri is necessary for distinguishing this tile
string tileUri = string.Concat("/MainPage.xaml?", SecondaryTileUriSource);
ShellTile.Create(new Uri(tileUri, UriKind.Relative), tileData);
}
}
NOTE: It is very important to set the NavigationUri of the Tile to something unique for the given Tile, so that you can easily find it later. In our case we set the NavigationUri to "/MainPage.xaml?Source=SeconaryTile" ("Source=SeconaryTile" is stored in the SecondaryTileUriSource constant).
Then whenever we need to find this Tile we use the SecondaryTileUriSource constant.
Step6. When the CheckBox is unchecked the secondary Tile is deleted.
NOTE: A secondary Tile can be deleted by:
-
The user unpinning the Tile from Start.
-
Uninstalling the application.
-
Calling Delete.
private void cbShowTile_Unchecked(object sender, RoutedEventArgs e)
{
ShellTile tile = this.FindTile(SecondaryTileUriSource);
if (tile != null)
{
tile.Delete();
MessageBox.Show("Secondary tile deleted.");
}
}
Step7.Override OnNavigatedTo and set the IsChecked property of the CheckBox based on whether there is a secondary Tile or not.
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
base.OnNavigatedTo(e);
ShellTile secondaryTile = this.FindTile(SecondaryTileUriSource);
this.cbShowTile.IsChecked = secondaryTile != null;
}
That was all about how to add and remove secondary Tiles in Windows Phone apps. The full source code is available here:
I hope that the article was helpful.
You can also follow us on Twitter @winphonegeek
Comments
Article was really helpful
posted by: Ilija Injac on 2/2/2012 2:53:10 PM
Hi, the article was quite helpful and interesting. It helped me a lot to understand secondary tiles. Regards, Ilija.
???
posted by: jj on 3/19/2012 11:44:15 PM
Hi, I can have more than one secondary title?
New! WindowsPhoneGeek Component Marketplace
Our Top Articles & Free books
- Our FREE e-book: "Windows Phone Toolkit In Depth" 2nd edition
- 400+ Windows Phone Development articles in our Article Index
- 21 WP7 Toolkit in Depth articles covering all controls
- 12 WP7 Coding4Fun Toolkit in Depth articles covering all controls
- Performance Tips when creating WP7 apps
- Creating a WP7 Custom Control in 7 Steps
- WP7 working with VisualStates: How to make a ToggleSwitch from CheckBox
- What makes a WP7 App successful
- Creating theme friendly UI in WP7 using OpacityMask
- Implementing Windows Phone 7 DataTemplateSelector and CustomDataTemplateSelector
Our Top Tips & Samples
- All about WP7 Isolated Storage series
- WP7 Dynamically Generating DataTemplate in code
- 5 tips for a successful WP7 Marketplace submission
- WP7: Navigating to a page in different assembly
- WP7 ContextMenu: answers to popular questions
- WP7 ListBox: answers to popular questions
- WP7 working with Images: Content vs Resource build action
- WP7 Element Binding samples
- WP7 working with XML: reading, filtering and databinding
- Drawing in WP7: #2 Drawing shapes with finger
- WP7 TextBox Light theme problems - the solution
- Changing the WP7 Panorama Background Image dynamically with Animation
