1

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 an 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)
    {
        //vibrate
        if (Settings.EnableVibration.Value)
        {
            VibrateController.Default.Start(TimeSpan.FromMilliseconds(40));
        }

        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

So I have tried to create a tap property for each tile, and then when a tile is tapped, the event handler decides which tile was the tapped one and navigates to a new page accordingly. For some reason the Tap property is null though? The project loads in the emulator but a tile tap does not work, and on my actual device the app doesnt load at all?

4

3 回答 3

1

将点击事件处理程序更改为以下以正确实现:

private void hubTile_Tap(object sender, System.Windows.Input.GestureEventArgs e)
    {
        //vibrate
        if (Settings.EnableVibration.Value)
        {
            VibrateController.Default.Start(TimeSpan.FromMilliseconds(40));
        }

        //TileItem tap = sender as TileItem;
        HubTile tap = sender as HubTile;
        string _tap = tap.Title.ToString();  //NullReferenceException occurs here

        switch(_tap)
        {
            //case "shareStatus_Tap":
            case "status":
                this.NavigationService.Navigate(new Uri("/Views/ShareStatusPage.xaml", UriKind.Relative));
                break;
            //case "shareLink_Tap":
            case "link":
                this.NavigationService.Navigate(new Uri("/Views/ShareLinkPage.xaml", UriKind.Relative));
                break;
        }
    }
于 2012-08-14T18:08:29.640 回答
1

您收到NullReferenceException是因为您的TileItem实例的Tap属性可能未设置。相反,您设置的是HubTile控件中Tap事件的处理程序。

现在,为了获得你想要的行为,我建议如下:

  1. 您应该添加NavigationUri属性,而不是TileItem上的Tap属性。
  2. 在HubTile的Tap事件的事件处理程序中,您可以使用NavigationUri直接导航到相应的视图,而不是检查哪个图块被点击

以下是它的工作原理:

public class TileItem
{
    // previous properties (except Tap, as you don't need it)
    ...

    public string NavigationUri {get; set;}
}

对于每个TileItem,给它相应的NavigationUri

var shareStatusPage= new TileItem 
                     {
                        Title="Share Status",
                        ImageUri="ShareStatus.png", 
                        NavigationUri="/Views/ShareStatusPage.xaml", 
                        //rest of properties
                     };
var shareLinkPage= new TileItem 
                     {
                        Title="Share Link",
                        ImageUri="ShareLink.png", 
                        NavigationUri="/Views/ShareLinkPage.xaml", 
                        //rest of properties
                     };

简化的Tap事件处理程序变为:

private void hubTile_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
    //vibrate
    if (Settings.EnableVibration.Value)
    {
        VibrateController.Default.Start(TimeSpan.FromMilliseconds(40));
    }

    TileItem item = sender as TileItem;
    this.NavigationService.Navigate(new Uri(item.NavigationUri, UriKind.Relative));
}

希望这可以帮助 :)

于 2012-08-14T18:23:29.317 回答
1

您不需要在 xaml 中绑定 tap 属性,而是创建一个 tap 处理程序并将处理程序的名称提供给 tap 属性。

于 2012-08-14T05:28:44.463 回答