EziData Solutions

Web, NET and SQL Server

Responding to Windows Phone Toolkit HubTile Events

In a previous post we looked at how to include the metro-styled HubTile to your Windows Phone app. In this post, we'll discover how to perform an action when the user taps the tile. 

The HubTile contains an event named Tap, which is triggered whenever the users touches the tile. This event can be handled in your code-behind to perform a task, such as navigating to a new page in your app. 

XAML

<toolkit:HubTile  
        Title="Sam"  
        Message="Yosemite Sam"  
        Source="/Assets/Images/Yosemite-Sam.jpg"  
        Background="{StaticResource PhoneAccentBrush}" 
        Tap="HubTile_Tap"/>

Code Behind

private void HubTile_Tap(object sender, System.Windows.Input.GestureEventArgs e) 
{ 
    NavigationService.Navigate(new Uri("/DatailsPage.xaml"UriKind.Relative)); 
}

This solution works fine if you have a single HubTile, but what about if you have more than one HubTile, which really is the point of the control in the first place? 

The good news is that there are a number of way you can handle multiple HubTile on a single page. Firstly, you could create separate handlers for each Tap event, like in the example below.

XAML

<toolkit:HubTile  
        Title="Sam"  
        Message="Yosemite Sam"  
        Source="/Assets/Images/Yosemite-Sam.jpg"  
        Background="{StaticResource PhoneAccentBrush}" 
        Tap="HubTile1_Tap"/> 
<toolkit:HubTile  
        Title="Bugs"  
        Message="Bugs Bunny"  
        Source="/Assets/Images/Bugs-Bunny.jpg"  
        Background="{StaticResource PhoneAccentBrush}" 
        Tap="HubTile2_Tap" 
    Margin="12,0,0,0"/>

Code Behind

private void HubTile1_Tap(object sender, System.Windows.Input.GestureEventArgs e) 
{ 
    NavigationService.Navigate(new Uri("/DatailsPage1.xaml"UriKind.Relative)); 
} 
  
private void HubTile2_Tap(object sender, System.Windows.Input.GestureEventArgs e) 
{ 
    NavigationService.Navigate(new Uri("/DatailsPage2.xaml"UriKind.Relative)); 
}

This gives you a nice separation of concerns and makes it clear exactly what is going on, but it also means there's a lot of code.

Another option is to use the single event handler, but to somehow identify the actually HubTile that was tapped. The sender object in the HubTile Tap event is the HubTile that was tapped, so it is relatively easy to cast the sender but to a HubTile and retrieve whatever values you want to uniquely identify the HubTile.

The code below shows how easy it is to retrieve the HubTile's Title value and then use it in a switch statement to perform a different action for each HubTile.

private void HubTile_Tap(object sender, System.Windows.Input.GestureEventArgs e) 
{ 
    HubTile tile = (HubTile)sender; 
    MessageBox.Show(tile.Title); 
  
    switch(tile.Title) 
    { 
        case "Sam": 
            NavigationService.Navigate(new Uri("DetailsPage1.xaml",UriKind.Relative)); 
            break; 
        case "Bugs": 
            NavigationService.Navigate(new Uri("DetailsPage1.xaml",UriKind.Relative)); 
            break; 
    } 
}
Posted: Sep 30 2011, 14:54 by CameronM | Comments (0) RSS comment feed |
  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Filed under: Windows Phone 7 | WP7