我从windows phone工具包中实现了HubTile控件的以下实现,除了从选定的磁贴实现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)
{
//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
所以我尝试为每个图块创建一个tap属性,然后在点击一个图块时,事件处理程序决定哪个图块是点击的图块并相应地导航到新页面。出于某种原因,Tap
属性为空?该项目在模拟器中加载,但是瓷砖点击不起作用,而在我的实际设备上,应用程序根本没有加载?
答案 0 :(得分:1)
您不需要在xaml中绑定tap属性,而是创建一个tap处理程序,并为tap属性提供处理程序的名称。
答案 1 :(得分:1)
将tap事件处理程序更改为以下内容以正确实现:
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;
}
}
答案 2 :(得分:1)
您收到 NullReferenceException ,因为可能未设置 TileItem 实例的 Tap 属性。您正在设置的是 HubTile 控件中点击事件的处理程序。
现在,为了获得您想要的行为,我建议如下:
以下是它的工作原理:
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));
}
希望这会有所帮助:)