C# 如何使用WP8C导航字符串消息和列表并将其检索到另一个页面#
在我的windows phone应用程序中,我有两个页面C# 如何使用WP8C导航字符串消息和列表并将其检索到另一个页面#,c#,windows-phone-8,C#,Windows Phone 8,在我的windows phone应用程序中,我有两个页面CreateGroups和GroupOfContacts,我想使用如下按钮将列表和字符串消息从CreateGroups导航到GroupOfContacts页面: private void btn_Click(object sender, RoutedEventArgs e) { List<CustomContact> contact = new List<CustomContact>(
CreateGroups
和GroupOfContacts
,我想使用如下按钮将列表和字符串消息从CreateGroups
导航到GroupOfContacts
页面:
private void btn_Click(object sender, RoutedEventArgs e)
{
List<CustomContact> contact = new List<CustomContact>();
PhoneApplicationService.Current.State["contactlist"] = contact;
NavigationService.Navigate(new Uri("/GroupOfContacts.xaml?msg=" + buttonName, UriKind.RelativeOrAbsolute));
NavigationService.Navigate(new Uri("/GroupOfContacts.xaml?contactlist=" + contact, UriKind.RelativeOrAbsolute));
}
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
base.OnNavigatedTo(e);
string msg = "";
if (NavigationContext.QueryString.TryGetValue("msg", out msg))
{
tblk_GroupName.Text = msg;
}
if (PhoneApplicationService.Current.State["contactlist"] != null)
{
listcontact = (List<CustomContact>)(PhoneApplicationService.Current.State["listOfContact"]);
}
}
但在这一行,它给我带来了异常listcontact=(List)(PhoneApplicationService.Current.State[“listOfContact]”)代码>
例外情况如下:
A first chance exception of type 'System.Collections.Generic.KeyNotFoundException' occurred in mscorlib.ni.dll
Additional information: The given key was not present in the dictionary.
有没有办法解决这个问题,或者建议如何使用WP8c#导航字符串消息和列表,并从其他页面检索。帮我等你的答复。
谢谢。当您需要使用NavigationService传递多个参数时,您应该使用“&”。
但是这里你需要传递一个列表。所以这还不够
如果这有帮助,试试看
private void hyplnk_Next_Click(object sender, RoutedEventArgs e)
{
NavigationService.Navigate("/GroupOfContacts.xaml?msg=" +buttonName+ "&listOfContact=1", listOfContact);
}
然后在另一个页面上访问它,如::
protected override void OnNavigatedTo(NavigationEventArgs e)
{
// Store test data.
List<CustomContact> listContacts = new List<CustomContact>();
string msg;
if (NavigationContext.QueryString.TryGetValue("msg", out msg))
{
tblk_GroupName.Text = msg;
}
// Request parameter. The identification of the source page.
string parameter = NavigationContext.QueryString["listOfContact"];
switch (parameter)
{
case "1":
var myParameter = NavigationService.GetLastNavigationData();
if (myParameter != null)
{
listContacts = (List<CustomContact>)myParameter;
}
break;
}
}
受保护的覆盖无效OnNavigatedTo(NavigationEventArgs e)
{
//存储测试数据。
列表联系人=新列表();
串味精;
if(NavigationContext.QueryString.TryGetValue(“msg”,out msg))
{
tblk_GroupName.Text=msg;
}
//请求参数。源页面的标识。
字符串参数=NavigationContext.QueryString[“listOfContact”];
开关(参数)
{
案例“1”:
var myParameter=NavigationService.GetLastNavigationData();
if(myParameter!=null)
{
listContacts=(列表)myParameter;
}
打破
}
}
希望有帮助:)您还可以将App类(App.xaml.cs)中的变量定义为公共静态变量,并使用
应用程序按钮名称打字错误<代码>状态[“联系人列表”]
应该是状态[“联系人列表”]
?