【发布时间】:2014-01-29 19:14:25
【问题描述】:
在window phone中,我使用以下代码在页面之间传输数据,
NavigationService.Navigate(new Uri("/Page.xaml?object1=" & obj, UriKind.Relative));
这里我在页面之间传递一个对象,我应该怎么做才能在页面之间传递两个对象??
【问题讨论】:
标签: c# windows-phone-7
在window phone中,我使用以下代码在页面之间传输数据,
NavigationService.Navigate(new Uri("/Page.xaml?object1=" & obj, UriKind.Relative));
这里我在页面之间传递一个对象,我应该怎么做才能在页面之间传递两个对象??
【问题讨论】:
标签: c# windows-phone-7
这个问题的答案是更好的解决方案:Passing data from page to page
代码:
PhoneApplicationService.Current.State["MyObject"] = yourObject;
NavigationService.Navigate(new Uri("/view/Page.xaml", UriKind.Relative));
//In the Page.xaml-page
var obj = PhoneApplicationService.Current.State["MyObject"];
您可以只在 URL 中添加参数,如下所示:
NavigationService.Navigate(new Uri("/Page.xaml?object1=" + obj + "&object2=" + obj2, UriKind.Relative));
否则,创建一个包含所有对象的包装器对象(就像在 MVVM 模式中使用的那样):
public class Container
{
public object Object1 { get; set; }
public object Object2 { get; set; }
}
var container = new Container { Object1 = obj, Object2 = obj2 };
NavigationService.Navigate(new Uri("/Page.xaml?object1=" + container, UriKind.Relative));
【讨论】:
protected override void OnNavigatedTo(NavigationEventArgs e) { //code with the Container like the Abbas answer }
我不确定你所说的对象是什么意思。您是指继承自 Object 的 ACTUAL 对象,还是指诸如 String 值或 int 值之类的值。
不管:
NavigationService.Navigate(new Uri("/Page.xaml?object1="+obj+"&object2="+obj2, UriKind.Relative));
这应该适合你。
【讨论】: