Silverlight 3 Beta,ViewModel 中的 NavigationService
我正在开发一个 silverlight 3 beta 导航应用程序,因此我对 MVVM 模式进行了轻微的修改:)(一体化视图模型),使用了 prism 等。
问题:如何导航到视图模型中的不同“NavigationPage”
现在长话短说,视图模型被声明为页面资源。
<navigation:Page.Resources>
<mvvm:LoginModel x:Key="DataSource" d:IsDataSource="True"></mvvm:LoginModel>
</navigation:Page.Resources>
然后使用命令将所有内容与视图模型连接
<Button x:Name="LoginButton" Width="100" Margin="8" Content="Login"
prism:Click.Command="{Binding LoginCommand}"/>
现在,如果我尝试导航视图模型中的任何位置,那么
this.NavigationService.Navigate(new Uri("/Views/About.xaml", UriKind.Relative));
导航服务为空,我环顾四周并发现 这篇文章,描述了使用 helix 0.3 进行导航,这是在 sl2 中构建的天,当导航控件从未存在时,现在 helix 的模型运行良好,通过在视图模型中实现 INavigationAware,您可以访问 NavigationContext,然后执行您需要的任何操作,我尝试过 helix,它可以工作。
SL3 带有内置的导航支持,可以说,它的功能与 helix 的功能完全相同。 所以我不想使用第三方框架,而是更喜欢使用内置的 sl3 功能。
SL3 中是否有任何东西可以模拟螺旋的 INavigationAware 界面?
im developing a silverlight 3 beta navigation application, so i've gone with a slight variation of the MVVM pattern :) (all-in-one viewmodel), using prism, and stuff.
Question: How do i navigate to a different "NavigationPage" in the viewmodel
Now to cut a long story short, the viewmodel is declared as a page resource.
<navigation:Page.Resources>
<mvvm:LoginModel x:Key="DataSource" d:IsDataSource="True"></mvvm:LoginModel>
</navigation:Page.Resources>
And then a command is used to wireup everything with the viewmodel
<Button x:Name="LoginButton" Width="100" Margin="8" Content="Login"
prism:Click.Command="{Binding LoginCommand}"/>
Now if i try to navigate anywhere in the viewmodel like so
this.NavigationService.Navigate(new Uri("/Views/About.xaml", UriKind.Relative));
the Navigationservice is null, i've looked around and found this article, which describes using helix 0.3 for navigation, this was built back in the sl2 days, when the navigation controls never existed, now helix's model works well, and by implementing INavigationAware in the viewmodel, you are able to gain access to the NavigationContext, and then do whatever it is you require, i have tried helix, and it works.
SL3 comes with the builtin Navigation support, so to speak, which does exactly what helix does. So i dont wanna use a 3rd party framework, instead i prefer to use the built in sl3 features.
Is there anything in SL3 that emulates the helix's INavigationAware interface?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我个人认为 NavigationService 是一个与 UI 框架或页面相关的 UI 概念。
无需将 NavigationService 传递到视图模型中即可实现此目的的另一种方法是让 ViewModel 在应该发生导航时引发一个事件...让视图处理视图模型事件并调用 Navigate 作为响应。
I personally think NavigationService is a UI-concept associated with the UI Frame or Page.
Another way to accomplish this without having to pass in a NavigationService into the view model is to have the ViewModel raise an event when navigation is supposed to occur... have the view handle the view model event and call Navigate in response.
一个狡猾的修复,但我唯一能用来让它工作的东西。
在视图中的OnNavigedTo事件中,访问ViewModel并将NavigationService设置为viewmodel中的属性,以便稍后可以在viewmodel中使用
A dodgy fix, but the only thing i've been able to use to get this working.
In the OnNavigatedTo event in the view, access the ViewModel and set the NavigationService to a property in the viewmodel so that it can be used later in the viewmodel
如果您使用 MVVM light,您可能需要考虑使用消息系统。 在您的页面上有一个侦听器来托管执行导航并从视图模型发送导航消息的框架。
You may want to consider using the Messaging system if you are using MVVM light. Have a listener on your page hosting the frame that does the navigation and send the nav messages from your view models.
可以帮助我解决问题,因为仍然没有任何答案,我将提供更多信息。
这是视图模型中的代码
NavigationService 为空,因此我无法移动到下一个视图,救命!!!
okay to help my question along, cos there still hasn't been any answer, i'm gonna throw more information at it.
This is the code in the viewmodel
NavigationService is null, therefore i cant move to the next view, help!!!
以上应该有效。
Above should work.