使用导航时将值从另一个页面传递到 Silverlight 中的 MainPage.xaml?
我在 Silverlight 中有一个从 MainPage.xaml 导航的页面,称为 OpenPage.xaml,然后我想将一个值传递回 MainPage.xaml - 使用以下命令调用此 OpenPage.xaml:
NavigationService.Navigate(new Uri("/OpenPage.xaml", UriKind.Relative));
从主页 - 这不是MainPage 的子级作为 RootVisual 被替换 - 我可以这样称呼:
NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
要返回到 MainPage - 但是我需要将值从 OpenPage.xaml 传递到 MainPage.xaml - 如何访问 MainPage 实例 - 我有一个名为 Document 的属性,但是当我这样做时:
MainPage m = (MainPage)Application.Current.RootVisual;
m.Document = "Hello World";
或者这样:
((MainPage)root).Document = "Hello World";
我收到无效的强制转换异常,因为我认为它正在尝试将 OpenPage.xaml 强制转换为 MainPage.xaml - 如何获取 NavigatedTo 页面,我想将该属性设置为来自 OpenPage.xaml 的 MainPage.xaml。
我还想将值从 MainPage.xaml 传递到另一个页面 SavePage.xaml - 但这有同样的问题 - 我该如何解决这个问题?
I have a Page in Silverlight which is Navigated from the MainPage.xaml, called OpenPage.xaml, I then want to pass a value back to the MainPage.xaml - this OpenPage.xaml is called using this:
NavigationService.Navigate(new Uri("/OpenPage.xaml", UriKind.Relative));
From the mainpage - this is not a child of the MainPage as the RootVisual is replaced - I can call this:
NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
To return to the MainPage - however I need to pass a value from OpenPage.xaml to the MainPage.xaml - how to I access the MainPage instance - I have a Property called Document however when I do this:
MainPage m = (MainPage)Application.Current.RootVisual;
m.Document = "Hello World";
Or this:
((MainPage)root).Document = "Hello World";
I get an invalid cast exception because I think it is trying to cast the OpenPage.xaml to MainPage.xaml - how to I get the NavigatedTo Page, I want to set the property on MainPage.xaml from OpenPage.xaml.
I also want to pass values from the MainPage.xaml to another page SavePage.xaml - but this has the same issue - how do I resolve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用查询字符串值:-
MainPage
然后可以使用以下方式获取该值:-编辑:
响应重新传递其他类型的注释。
一旦完成上述操作,您就可以使用其他服务模式来传递其他类型。例如,考虑一个实现以下功能的 MessageService: -
然后,您将实现此接口并将实现公开为单例: -
您的 OpenPage 调用
MessageService.Default.Store
并将生成的 Guid 放入查询字符串中。然后,MainPage 测试是否存在此类查询字符串值,如果存在,则使用其值调用
MessageService.Default.Retrieve
,从而从服务中删除该项目。Use a query string value:-
MainPage
can then acquire this value using:-Edit:
In response to comment re passing other types.
Once you have the above inplace you can use other service patterns to pass other types. For example consider a MessageService that implements:-
You would then implement this interface and expose the implementation as singleton say:-
Your OpenPage calls
MessageService.Default.Store
and places the resulting Guid in the query string.The MainPage then tests for the presence of such a query string value, if present uses its value to call
MessageService.Default.Retrieve
which removes the item from the service.