我从windows phone工具包中实现了HubTile控件的以下实现,除了从选定的磁贴实现tap事件外,一切正常。我不确定如何将特定磁贴的点击链接到后面的代码中的Tap事件处理程序(这应该只是导航到我的项目中的新页面)。我到目前为止的内容如下:
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
在模拟器中MainPage加载活动的hubtiles工作,但tap事件导致NullReferenceException,而在我的实际设备上,应用程序根本没有加载?
答案 0 :(得分:2)
将您的断点放在hubTile_Tap
中,看看sender
是什么。我猜这将是HubTile
,而不是TileItem
。在这种情况下,TileItem
可能是HubTile.DataContext
。
答案 1 :(得分:0)
我的写作和@JohnGardner一样,所以我会跳过它。请小心发件人。实际上在某些情况下它可能是任何事情。如果遇到这种情况,请尝试检查System.Windows.Input.GestureEventArgs e
及其Source
和OriginalSource
- 它们可能不一定是HubTile,并且没有必要的有趣的DataContext,但它们几乎可以保证它们是Visual
或UIElement
,您可以轻松地向上追踪他们的VisualParents,并且一个关闭的可视父级将是您的DataContext = TileItem的HubTile。这是最糟糕的情况。首先,尝试约翰所说的,你有95%它会起作用:)