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

4

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个回答

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

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

0
如果您正在使用MVVM架构,则可以在使用Messenger注册后传递navigationPage。 创建一个模型类(称为NavigateToPageMessage),其中包含一个字符串变量(称为PageName)。 如果要从homepage.xaml传递字符串到newpage.xaml,则只需在Homepage视图模型中绑定的命令下发送此消息,如以下示例(假设为HomeNavigationCommand):
private void HomeNavigationCommandHandler()
        {
            Messenger.Default.Send(new NavigateToPageMessage {PageName = "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;
        }

//假设你的视图在View文件夹中


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接