0

I have the following implementation of the HubTile control from the windows phone toolkit, and everything is working correctly except for implementing a tap event from the selected tile. I am unsure of how to link the tap of a specific tile to a Tap event handler in code behind (which should simply navigate to a new page in my project). What I have so far is as follows:

MainPage.xaml

<ListBox Grid.Row="0" x:Name="tileList" toolkit:TiltEffect.IsTiltEnabled="True">
                <ListBox.ItemsPanel>
                    <ItemsPanelTemplate>
                        <toolkit:WrapPanel Orientation="Horizontal" />
                    </ItemsPanelTemplate>
                </ListBox.ItemsPanel>
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <toolkit:HubTile Title="{Binding Title}" Margin="3"
                                         Notification="{Binding Notification}"
                                         DisplayNotification="{Binding DisplayNotification}"
                                         Message="{Binding Message}"
                                         GroupTag="{Binding GroupTag}"
                                         Source="{Binding ImageUri}"
                                         Tap="hubTile_Tap">
                        </toolkit:HubTile>

                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>

TileItem.cs

public class TileItem
{
    public string ImageUri
    {
        get;
        set;
    }

    public string Title
    {
        get;
        set;
    }

    public string Notification
    {
        get;
        set;
    }

    public bool DisplayNotification
    {
        get
        {
            return !string.IsNullOrEmpty(this.Notification);
        }
    }

    public string Message
    {
        get;
        set;
    }

    public string GroupTag
    {
        get;
        set;
    }

    //not sure how to implement this?
    public string Tap
    {
        get;
        set;
    }
}

MainPage.xaml.cs

    #region Ctr

    public MainPage()
    {
        InitializeComponent();

        CreateHubTiles();
    }

    #endregion

    private void CreateHubTiles()
    {
        List<TileItem> tileItems = new List<TileItem>() 
        {
            //there will be at least 2 distinct tiles linking to seperate pages
            new TileItem() { ImageUri = "/Images/shareStatusImage.jpg", Title = "status", /*Notification = "last shared link uri",*/ Message = "last shared status message", GroupTag = "TileGroup", Tap = "shareStatus_Tap" },
            new TileItem() { ImageUri = "/Images/shareLinkImage.jpg", Title = "link", /*Notification = "last shared status message",*/ Message = "last shared link uri", GroupTag = "TileGroup", Tap = "shareLink_Tap" }
        };

        this.tileList.ItemsSource = tileItems;
    }

    #region Navigation

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        base.OnNavigatedTo(e);

        HubTileService.UnfreezeGroup("TileGroup");
    }

    protected override void OnNavigatedFrom(NavigationEventArgs e)
    {
        base.OnNavigatedFrom(e);

        HubTileService.FreezeGroup("TileGroup");
    }

    #endregion

    #region Event Handlers

    private void hubTile_Tap(object sender, System.Windows.Input.GestureEventArgs e)
    {
        TileItem tap = sender as TileItem;
        string _tap = tap.Tap.ToString();  //NullReferenceException occurs here

        switch(_tap)
        {
            case "shareStatus_Tap":
                this.NavigationService.Navigate(new Uri("/Views/ShareStatusPage.xaml", UriKind.Relative));
                break;
            case "shareLink_Tap":
                this.NavigationService.Navigate(new Uri("/Views/ShareLinkPage.xaml", UriKind.Relative));
                break;
        }
    }
    #endregion

In the emulator the MainPage loads with the active hubtiles working but the tap event causes a NullReferenceException, while on my actual device the applicaion does not load at all?

Matthew
  • 3,976
  • 15
  • 66
  • 130

2 Answers2

2

put your breakpoint in hubTile_Tap and see what sender is. I'm guessing it will be the HubTile, not the TileItem. The TileItem is probably HubTile.DataContext in this case.

John Gardner
  • 24,225
  • 5
  • 58
  • 76
0

I was just writing the same as @JohnGardner, so I'll skip that. Just be careful with the Sender. Actually in some cases it may be quite anything. If you hit such case, then try inspecting the System.Windows.Input.GestureEventArgs e and its Source and OriginalSource - they may not necessarily be HubTile, and not necesasrily have interesting DataContext, but they are almost guaranteed that they are a Visual or UIElement and that you can easily trace their VisualParents upwards, and a close visual parent will be the HubTile with your DataContext=TileItem. This is the worst case. At first, try what John said, you've got 95% that it will work :)

quetzalcoatl
  • 32,194
  • 8
  • 68
  • 107
  • thats why i suggested putting a breakpoint there to make sure. yeah, you can get some wierd cases, especially depending on how complicated the thing inside the template is... – John Gardner Aug 14 '12 at 20:13