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

时间:2010-05-09 10:19:33

标签: mvvm windows-phone-7 navigation

Windows Phone 7中的导航框架是Silverlight中的简化版本。您只能导航到Uri而不能传入视图。由于NavigationService与View绑定,因此人们如何使其适应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属性来获取视图然后显示它。但是使用NavigationService,我必须传入一个视图Uri。我无法先创建视图模型。有没有办法解决这个问题。

4 个答案:

答案 0 :(得分:4)

而不是通过构造函数传递视图。您可以先通过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;
}

答案 1 :(得分:2)

答案 2 :(得分:0)

我的观点是应该在应用程序启动时创建和注册视图模型。通过将它放在根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;
    }

答案 3 :(得分:0)

如果您使用的是MVVM架构,则可以在使用Messenger注册后传递navigationPage。使用字符串(比如PageName)变量创建模型类(比如NavigateToPageMes​​sage)。你想把字符串从homepage.xaml传递到newpage.xaml,然后在Homepage viewmodel中只是在你绑定的命令下发送这样的消息(比如HomeNavigationCommand)

private void HomeNavigationCommandHandler()
        {
            Messenger.Default.Send(new NavigateToPageMessage {PageName = "newpage"});
        }

在新页面Viewmodel中,您应该注册这样的信使,

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;
        }

//假设您的观点位于“查看文件夹”