Windows Phone 7 Secondary Tile Navigation

Introduction

There are two kinds of Tile in Windows phone application: Application Tile and Secondary Tile. Each application has one and only one Application for users to launch the application while it is supported to create multiple secondary tiles through users’ certain operation. In some situations, we want to detect from which secondary tile user are navigated to the application.

The following points are covered in this sample:

  • How to create secondary tile
  • How to get detect from which secondary tile user are navigated to the application

Building the Sample

This sample needs be opened with Windows phone SDK 7.1 +, please download from below link.

Windows Phone SDK 7.1

Running the Sample

1. Open the CSWP7SecondaryTile.sln file with Visual Studio or Expression Blend.

2. Press Ctrl + F5 to run the sample.

3. Click “Create 1st Secondary Tile” button on the application.

4. Back to the application and Click “Create 2nd Secondary Tile” button.

5. Click on Back button of Windows Phone Device/ Windows Phone Emulator

6. Click on the Tile with FirstTile text.

7. You will see the Text shown as “Welcome back from FirstTile.”

8. Click on the Tile with SecondTile text.

9. You will see the Text shown as “Welcome back from SecondTile.”

Using the Code

1. How to create a Secondary Tile

 
Private Sub button_Click(sender As Object, e As RoutedEventArgs) 
    Dim tileParameter As String = "Param=" + DirectCast(sender, Button).Name 
    Dim tile As ShellTile = CheckIfTileExist(tileParameter) 
    If tile Is Nothing Then 
  
        Dim secondaryTile As New StandardTileData With { 
         .Title = tileParameter, 
         .BackgroundImage = New Uri("Background-Secondary.png", UriKind.Relative), 
         .Count = 2, 
         .BackContent = "Secondary Tile Test" 
        } 
        ShellTile.Create(New Uri("/MainPage.xaml?" & tileParameter, UriKind.Relative), secondaryTile) 'Create SecondaryTile and pass querystring to navigation url. 
    End If 
End Sub 
  
Private Function CheckIfTileExist(tileUri As StringAs ShellTile 
    Dim shellTile__1 As ShellTile = ShellTile.ActiveTiles.FirstOrDefault(Function(tile) tile.NavigationUri.ToString().Contains(tileUri)) 
    Return shellTile__1 
End Function 
private void button_Click(object sender, RoutedEventArgs e) 
{ 
    string tileParameter = "Param=" + ((Button)sender).Name;//Use Button.Name to mark Tile 
    ShellTile tile = CheckIfTileExist(tileParameter);// Check if Tile's title has been used 
    if (tile == null) 
    { 
        StandardTileData secondaryTile = new StandardTileData 
        { 
            Title = tileParameter, 
            BackgroundImage = new Uri("Background-Secondary.png", UriKind.Relative), 
            Count = 2, 
            BackContent = "Secondary Tile Test" 
  
        }; 
        ShellTile.Create(new Uri("/MainPage.xaml?" + tileParameter, UriKind.Relative), secondaryTile); // Pass tileParameter as QueryString 
    } 
} 
  
private ShellTile CheckIfTileExist(string tileUri) 
{ 
    ShellTile shellTile = ShellTile.ActiveTiles.FirstOrDefault( 
            tile => tile.NavigationUri.ToString().Contains(tileUri)); 
    return shellTile; 
}
 
 

2. Detect from which secondary tile user are navigated to the application by overriding OnNavigatedTo method

 
Protected Overrides Sub OnNavigatedTo(e As System.Windows.Navigation.NavigationEventArgs) 
    MyBase.OnNavigatedTo(e) 
    If Me.NavigationContext.QueryString.ContainsKey("Param"Then 
        Dim param As String = Me.NavigationContext.QueryString("Param")'Get "Param" this query string. 
        textBlock1.Text = "Welcome back from " & param 
    End If 
End Sub 
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) 
{ 
    base.OnNavigatedTo(e); 
    if (this.NavigationContext.QueryString.ContainsKey("Param")) 
    { 
        string param = this.NavigationContext.QueryString["Param"];//Get "Param" this QueryString. 
        textBlock1.Text = "Welcome back from " + param; 
    } 
}
 

More Information

Tiles Overview for Windows Phone

Last edited Feb 13, 2013 at 8:37 AM by super_J, version 2