Windows Phone 7 中的导航框架是 Silverlight 中的精简版。您只能导航到 Uri 而不能传入 View 。由于 NavigationService 与 View 绑定(bind),人们如何使其适合 MVVM。例如:
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 模型,然后使用 View 属性来获取 View ,然后再显示它。但是使用 NavigationService,我必须传入一个 View Uri。我没有办法先创建 View 模型。有没有办法解决这个问题。
最佳答案
而不是通过构造函数传递 View 。您可以先通过 NavigationService 构造 View 并将其传递给 View 模型。像这样:
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;
}
关于MVVM 在 Windows Phone 7 上使用页面导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2797318/