MVVM 在 Windows Phone 7 上使用页面导航

标签 mvvm windows-phone-7 navigation

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/

相关文章:

mvvm - 为什么在观察LiveData时未调用onChanged()

c# - 在mvvm模型中实现Windows Phone应用

c# - 首次启动应用程序时消息提示崩溃

jQuery Cycle - 如何将 'scrollHorz' 过渡(包括后退/下一步导航)与自定义不透明度效果结合使用?

html - 标题/导航问题

android - Xamarin 没有 Forms/MvvmCross/Data binding

mvvm - Treeview SelectedItem有时是VM,有时是TreeViewItem

c# - 如何在 WPF 中使用一次性 View 模型?

silverlight - 具有动态 3D 内容的自定义 WP7 Silverlight 控件

android - 抽屉导航中的用户帐户