从 Windows Phone 8.1 通用应用中的视图模型导航到新页面 [英] Navigating to a new page from the View Model in Windows Phone 8.1 universal app
问题描述
我正在开发 Windows Phone 8.1 通用应用程序,并希望找到处理页面导航的最佳方式,而无需在背后的代码中包含大量逻辑.我想让代码在我的视图中尽可能整洁.响应按钮点击而导航到新页面的公认 MVVM 方式是什么?
I am working on a windows phone 8.1 universal app and want to find the best way of handling page navigations without having large amounts of logic in the code behind. I want to keep the code behind in my View as uncluttered as possible. What is the accepted MVVM way of navigating to a new page in response to a button click?
我目前必须从 ViewModel 向视图发送 RelayComnmand 消息,其中包含要导航到的页面的详细信息.这意味着后面的代码必须按如下方式接线:
I currently have to send a RelayComnmand message from the ViewModel to the view with the details of the page to navigate to. This means that the code behind has to be wired up as follows:
public MainPage()
{
InitializeComponent();
Messenger.Default.Register<OpenArticleMessage>(this, (article) => ReceiveOpenArticleMessage(article));
...
}
private object ReceiveOpenArticleMessage(OpenArticleMessage article)
{
Frame.Navigate(typeof(ArticleView));
}
这似乎不是最好的方法,尽管它确实有效.如何直接从 ViewModel 进行页面导航?我在我的项目中使用 MVVM-Light.
This just doesn't seem the best way although it does work. How can I do the page navigations directly from the ViewModel? I am using MVVM-Light in my project.
推荐答案
好的,我已经找到了这个问题的答案.进行了一些调查,但我最终找到了首选的 MVVM-Light 方法.无论如何,我不相信这个答案,但只是将其张贴在这里,以防人们正在寻找这个问题的答案.
Ok, I have found an answer to this question. Took a bit of investigation but I eventually found the preferred MVVM-Light way of doing this. I don't take credit for this answer in anyway but just posting it here in case people are looking for an answer to this question.
创建一个INavigationService接口如下:
Create an INavigationService interface as follows:
public interface INavigationService
{
void Navigate(Type sourcePageType);
void Navigate(Type sourcePageType, object parameter);
void GoBack();
}
创建一个NavigationService类如下:
Create a NavigationService class as follows:
public class NavigationService : INavigationService
{
public void Navigate(Type sourcePageType)
{
((Frame)Window.Current.Content).Navigate(sourcePageType);
}
public void Navigate(Type sourcePageType, object parameter)
{
((Frame)Window.Current.Content).Navigate(sourcePageType, parameter);
}
public void GoBack()
{
((Frame)Window.Current.Content).GoBack();
}
}
现在在 ViewModelLocator 中,像这样设置:
Now in the ViewModelLocator, set it up like this:
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance",
"CA1822:MarkMembersAsStatic",
Justification = "This non-static member is needed for data binding purposes.")]
public MainViewModel Main
{
get
{
return ServiceLocator.Current.GetInstance<MainViewModel>();
}
}
static ViewModelLocator()
{
ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
if (ViewModelBase.IsInDesignModeStatic)
{
SimpleIoc.Default.Register<INavigationService, Design.DesignNavigationService>();
}
else
{
SimpleIoc.Default.Register<INavigationService>(() => new NavigationService());
}
SimpleIoc.Default.Register<MainViewModel>();
}
接下来为设计时设置导航服务,如下所示:
Next setup a navigation service for design time as follows:
public class DesignNavigationService : INavigationService
{
// This class doesn't perform navigation, in order
// to avoid issues in the designer at design time.
public void Navigate(Type sourcePageType)
{
}
public void Navigate(Type sourcePageType, object parameter)
{
}
public void GoBack()
{
}
}
我的 MainViewModel 构造函数如下:
My MainViewModel constructor is as follows:
public MainViewModel(INavigationService navigationService)
{
_navigationService = navigationService;
...
现在您可以简单地使用它在您的视图模型中导航:
Now you can simply use this to navigate in your viewmodel:
_navigationService.Navigate(typeof(WelcomeView));
有关原作者 Laurent Bugnion 的更多详细信息,请参阅本文和相关代码.http://msdn.microsoft.com/en-us/magazine/jj651572.aspx
For more details on the original author Laurent Bugnion see this article and associated code. http://msdn.microsoft.com/en-us/magazine/jj651572.aspx
这篇关于从 Windows Phone 8.1 通用应用中的视图模型导航到新页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!