Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
210 views
in Technique[技术] by (71.8m points)

c# - Navigating to a new page from the View Model in Windows Phone 8.1 universal app

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?

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

Create an INavigationService interface as follows:

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

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

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()
    {
    }
}

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...