下面是直接从我最新的应用程序中提取的。
不能直接传递对象,但可以传递文本数据。就我而言,一个ID:
private void WishListBoxSelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (WishListBox != null && WishListBox.SelectedItem != null)
{
var selectedItem = (Models.Gift)WishListBox.SelectedItem;
WishListBox.SelectedIndex = -1;
var id = selectedItem.Id;
NavigationService.Navigate(new Uri("/Views/Gift/GiftView.xaml?action=load&id=" + id, UriKind.Relative));
}
}
然后在接收端:
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
base.OnNavigatedTo(e);
if (NavigationContext.QueryString.ContainsKey("action"))
{
if (NavigationContext.QueryString["action"]=="load")
{
PageTitle.Text = "edit gift";
giftVm.Gift = App._context.Gifts.Single(g => g.Id == Int32.Parse(NavigationContext.QueryString["id"]));
}
else if (NavigationContext.QueryString["action"] == "new")
{
PageTitle.Text = "new gift";
}
else if (NavigationContext.QueryString["action"] == "newWishList")
{
App.vm = ((MainViewModel)App.vm).Me;
}
}
else
{
MessageBox.Show("NavigationContext.QueryString.ContainsKey('action') is false");
}
}
就我而言,数据存储在数据库中。我只需将所选项目转换为正确的对象类型,然后检查其 ID 并将其传递到我进行查找的下一页。
我希望这有帮助。