从Windows 8.1手机通用的应用程序的视图模型导航到一个新页面 [英] Navigating to a new page from the View Model in Windows Phone 8.1 universal app

查看:227
本文介绍了从Windows 8.1手机通用的应用程序的视图模型导航到一个新页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一款Windows Phone 8.1通用的应用程序,并希望找到处理页面导航的最佳途径,而无需在代码中大量的逻辑背后。我想在我查看整齐地背后保持代码。什么是响应一个按钮,点击导航到新页面的接受MVVM方式?

I am working on a windows phone 8.1 universal app and want to find the best way of handling page navigations without having large amounts of logic in the code behind. I want to keep the code behind in my View as uncluttered as possible. What is the accepted MVVM way of navigating to a new page in response to a button click?

目前,我有从视图模型发送RelayComnmand信息与观点页面细节导航到。这意味着,后面的代码已经进行接线如下:

I currently have to send a RelayComnmand message from the ViewModel to the view with the details of the page to navigate to. This means that the code behind has to be wired up as follows:

    public MainPage()
    {
      InitializeComponent();
      Messenger.Default.Register<OpenArticleMessage>(this, (article) => ReceiveOpenArticleMessage(article));
...
}

    private object ReceiveOpenArticleMessage(OpenArticleMessage article)
    {
     Frame.Navigate(typeof(ArticleView));
   }

这只是似乎不是最好的方法,虽然它的工作。我怎么能直接从视图模型做页面导航?我使用MVVM,光在我的项目。

This just doesn't seem the best way although it does work. How can I do the page navigations directly from the ViewModel? I am using MVVM-Light in my project.

推荐答案

好吧,我已经找到了这个问题的答案。采取了一些调查,但我最终发现这样做的首选MVVM光强的方法。我不邀功这个答案反正只是张贴在这里的情况下,人们都在寻找一个答案

Ok, I have found an answer to this question. Took a bit of investigation but I eventually found the preferred MVVM-Light way of doing this. I don't take credit for this answer in anyway but just posting it here in case people are looking for an answer to this question.

创建INavigationService界面如下:

Create an INavigationService interface as follows:

public interface INavigationService
{
    void Navigate(Type sourcePageType);
    void Navigate(Type sourcePageType, object parameter);
    void GoBack();
}



创建的NavigationService类,如下所示:

Create a NavigationService class as follows:

public class NavigationService : INavigationService
{
    public void Navigate(Type sourcePageType)
    {
        ((Frame)Window.Current.Content).Navigate(sourcePageType);
    }

    public void Navigate(Type sourcePageType, object parameter)
    {
        ((Frame)Window.Current.Content).Navigate(sourcePageType, parameter);
    }

    public void GoBack()
    {
        ((Frame)Window.Current.Content).GoBack();
    }
}

现在在ViewModelLocator,设置它是这样的:

Now in the ViewModelLocator, set it up like this:

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance",
        "CA1822:MarkMembersAsStatic",
        Justification = "This non-static member is needed for data binding purposes.")]
    public MainViewModel Main
    {
        get
        {
            return ServiceLocator.Current.GetInstance<MainViewModel>();
        }
    }

    static ViewModelLocator()
    {
        ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);

        if (ViewModelBase.IsInDesignModeStatic)
        {
            SimpleIoc.Default.Register<INavigationService, Design.DesignNavigationService>();
        }
        else
        {
            SimpleIoc.Default.Register<INavigationService>(() => new NavigationService());
        }

        SimpleIoc.Default.Register<MainViewModel>();
    }



下一步设置为设计时导航服务如下:

Next setup a navigation service for design time as follows:

public class DesignNavigationService : INavigationService
{
    // This class doesn't perform navigation, in order
    // to avoid issues in the designer at design time.

    public void Navigate(Type sourcePageType)
    {
    }

    public void Navigate(Type sourcePageType, object parameter)
    {
    }

    public void GoBack()
    {
    }
}

我MainViewModel构造如下:

My MainViewModel constructor is as follows:

   public MainViewModel(INavigationService navigationService)
    {
        _navigationService = navigationService;

        ...

现在你可以简单地使用这个在浏览您的视图模型:

Now you can simply use this to navigate in your viewmodel:

_navigationService.Navigate(typeof(WelcomeView));

有关原作者洛朗•比尼翁更多详细信息请参阅本文以及相关的代码。
http://msdn.microsoft.com/en-us/magazine/jj651572.aspx

For more details on the original author Laurent Bugnion see this article and associated code. http://msdn.microsoft.com/en-us/magazine/jj651572.aspx

这篇关于从Windows 8.1手机通用的应用程序的视图模型导航到一个新页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆