在WindowsPhone7上使用页面导航的MVVM
WindowsPhone7中的导航框架是Silverlight中的精简版本。您只能导航到Uri,不能在视图中传递。由于NavigationService与视图绑定,人们如何将其融入MVVM。例如:在WindowsPhone7上使用页面导航的MVVM,mvvm,windows-phone-7,navigation,Mvvm,Windows Phone 7,Navigation,WindowsPhone7中的导航框架是Silverlight中的精简版本。您只能导航到Uri,不能在视图中传递。由于NavigationService与视图绑定,人们如何将其融入MVVM。例如: public class ViewModel : IViewModel { private IUnityContainer container; private IView view; public ViewModel(IUnityContainer container, I
public class ViewModel : IViewModel
{
private IUnityContainer container;
private IView view;
public ViewModel(IUnityContainer container, IView view)
{
this.container = container;
this.view = view;
}
public ICommand GoToNextPageCommand { get { ... } }
public IView { get { return this.view; } }
public void GoToNextPage()
{
// What do I put here.
}
}
public class View : PhoneApplicationPage, IView
{
...
public void SetModel(IViewModel model) { ... }
}
我正在使用Unity IOC容器。我必须首先解析视图模型,然后使用view属性获取视图,然后显示它。但是使用NavigationService,我必须传入一个视图Uri。我没有办法先创建视图模型。有没有办法绕过这个问题。而不是通过构造函数传递视图。您可以首先通过NavigationService构建视图,并将其传递到视图模型中。像这样:
public class ViewModel : IViewModel
{
private IUnityContainer container;
private IView view;
public ViewModel(IUnityContainer container)
{
this.container = container;
}
public ICommand GoToNextPageCommand { get { ... } }
public IView
{
get { return this.view; }
set { this.view = value; this.view.SetModel(this); }
}
public void GoToNextPage()
{
// What do I put here.
}
}
PhoneApplicationFrame frame = Application.Current.RootVisual;
bool success = frame.Navigate(new Uri("View Uri"));
if (success)
{
// I'm not sure if the frame's Content property will give you the current view.
IView view = (IView)frame.Content;
IViewModel viewModel = this.unityContainer.Resolve<IViewModel>();
viewModel.View = view;
}
public类ViewModel:IViewModel
{
专用IUnityContainer容器;
个人观点;
公共视图模型(IUnityContainer容器)
{
this.container=容器;
}
public ICommand GoToNextPageCommand{get{…}
公共IView
{
获取{返回this.view;}
设置{this.view=value;this.view.SetModel(this);}
}
public void GoToNextPage()
{
//我在这里放什么。
}
}
PhoneApplicationFrame=Application.Current.RootVisual;
bool success=frame.Navigate(新Uri(“视图Uri”));
如果(成功)
{
//我不确定框架的内容属性是否会提供当前视图。
IView view=(IView)frame.Content;
IViewModel viewModel=this.unityContainer.Resolve();
viewModel.View=View;
}
如果您使用的是Mvvm Light,您可以尝试:
(参见类似帖子:)我的观点是,应该在应用程序启动时创建并注册视图模型。通过将它放在根DataContext中,所有页面都将自动获得对它的引用,而无需任何代码隐藏或IoC技巧
// Code to execute when the application is launching (eg, from Start)
// This code will not execute when the application is reactivated
private void Application_Launching(object sender, LaunchingEventArgs e)
{
m_ViewModel = new PrimaryViewModel(RootFrame) ;
RootFrame.DataContext = m_ViewModel;
}
// Code to execute when the application is activated (brought to foreground)
// This code will not execute when the application is first launched
private void Application_Activated(object sender, ActivatedEventArgs e)
{
m_ViewModel = new PrimaryViewModel(RootFrame) ;
m_ViewModel.Activated(PhoneApplicationService.Current.State);
RootFrame.DataContext = m_ViewModel;
}
如果您使用的是MVVM体系结构,则可以在使用Messenger注册后通过navigationPage。创建一个带有字符串(比如PageName)变量的模型类(比如NavigateToPageMessage)。您希望将字符串从homepage.xaml传递到newpage.xaml,然后在homepage viewmodel中,只需在绑定的命令(比如HomeNavigationCommand)下发送如下消息 在newpage Viewmodel中,您应该像这样注册messenger
Messenger.Default.Register<NavigateToPageMessage>(this, (action) => ReceiveMessage(action));
private object ReceiveMessage(NavigateToPageMessage action)
{
var page = string.Format("/Views/{0}.xaml", action.PageName);
NavigationService.Navigate(new System.Uri(page,System.UriKind.Relative));
return null;
}
Messenger.Default.Register(此,(操作)=>ReceiveMessage(操作));
私有对象接收消息(NavigateToPageMessage操作)
{
var page=string.Format(“/Views/{0}.xaml”,action.PageName);
NavigationService.Navigate(newsystem.Uri(page,System.UriKind.Relative));
返回null;
}
//假设您的视图位于视图文件夹中
Messenger.Default.Register<NavigateToPageMessage>(this, (action) => ReceiveMessage(action));
private object ReceiveMessage(NavigateToPageMessage action)
{
var page = string.Format("/Views/{0}.xaml", action.PageName);
NavigationService.Navigate(new System.Uri(page,System.UriKind.Relative));
return null;
}