在Windows 8中使用MVVMLight通过ViewModel进行页面导航

时间:2012-06-10 18:01:59

标签: mvvm-light windows-8

我上周刚开始使用mvvm light开发我的全新Windows 8应用程序。我熟悉mvvmlight WP7导航。我如何在Windows 8中实现相同的功能。任何人都可以建议在Windows 8中实现相同的更好方法。我找到了一个解决方案,我们在vm中覆盖onnavigated事件并处理导航到其他页面。但我认为这种方法已经过时了。任何人请指导我正确的实施。提前谢谢。

3 个答案:

答案 0 :(得分:15)

我知道这不是您可能正在寻找的确切答案,但这可能会为您提供一些探索的想法。

就我而言,我不是在使用MVVMLight - 而是我自己的简单MVVM实现。我使用BindableBase类(它带有默认的VS 2012 RC模板)来进行属性通知。我想,您可以使用MVVMLight为您提供一些基础设施,您可以使用下面的内容进行补充。

对于导航,我定义了一个类似于:

的界面
public interface INavigationService
{
    void Navigate(Type type);
    void Navigate(Type type, object parameter);
    void EnsureNavigated(Type pageType, object parameter);

    bool CanGoBack { get; }
    bool CanGoForward { get; }
    void GoBack();
    void GoForward(); 

    IView CurrentView { get; }
}

按如下方式实施:

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Windows.UI.Xaml.Controls;

public class NavigationService : INavigationService
{
    private readonly Frame _frame;

    public NavigationService(Frame frame)
    {
        _frame = frame;
        _frame.Navigated += OnFrameNavigated;
    }

    private void OnFrameNavigated(object sender, Windows.UI.Xaml.Navigation.NavigationEventArgs e)
    {
        var view = e.Content as IView;
        if (view == null)
            return;

        var navMsg = new NavigationMessage()
        {
            Sender = this,
            NewView = view,
            Parameter = e.Parameter,
            NavigationMode = (int)e.NavigationMode
        };

        EventManager.Current.Publish(navMsg);

        //Anything that the parent needs to be notified should happen in of after this method
        var viewModel = view.ViewModel;
        if (viewModel != null)
            viewModel.Initialise(e.Parameter);
    }

    public void Navigate(Type pageType)
    {
        DisposePreviousView();
        _frame.Navigate(pageType);
    }

    public void Navigate(Type pageType, object parameter)
    {
        DisposePreviousView();
        _frame.Navigate(pageType, parameter);
    }

    private void DisposePreviousView()
    {
        var currentView = this.CurrentView;
        var currentViewDisposable = currentView as IDisposable;
        if (currentViewDisposable != null)
        {
            currentViewDisposable.Dispose();
            currentViewDisposable = null;
        } //view model is disposed in the view implementation
    }

    public void EnsureNavigated(Type pageType, object parameter)
    {
        var currentView = this.CurrentView;
        if (currentView == null || currentView.GetType() != pageType)
        {
            Navigate(pageType, parameter);
        }
    }

    public IView CurrentView
    {
        get { return _frame.Content as IView; }
    }


    public bool CanGoBack
    {
        get { return _frame != null && _frame.CanGoBack; }
    }

    public void GoBack()
    {
        // Use the navigation frame to return to the previous page
        if (_frame != null && _frame.CanGoBack) _frame.GoBack();
    }

    public bool CanGoForward
    {
        get { return _frame != null && _frame.CanGoForward; }
    }

    public void GoForward()
    {
        // Use the navigation frame to return to the previous page
        if (_frame != null && _frame.CanGoForward) _frame.GoForward();
    }

}

IVIEW:

public interface IView : IDisposable
{
    IViewModel ViewModel { get; }
    void Refresh();
}

IViewModel:

public interface IViewModel : INotifyPropertyChanged, IDisposable
{
    void Initialise(object parameter);
    string ViewTitle { get; }
    void Refresh();
}

最后,在XAML页面中,定义Frame元素:

<Frame x:Name="ContentFrame" />

在页面的代码隐藏中:(在我看来,这只是一个丑陋的部分 - 但希望不会太糟糕):

var _navigationService = new NavigationService(this.ContentFrame);

您现在可以将_navigationService传递给viewmodel。在我的例子中,我在页面的代码隐藏中创建了viewmodel:

public HomePage()
{
    this.InitializeComponent();

    var _navigationService = NavigationService.GetFor(this.ContentFrame);

    DataContext = new HomePageViewModel(_navigationService);

}

希望这有帮助。

答案 1 :(得分:4)

最近阅读了Laurent Bugnion自己在使用MVVM Light Toolkit和Windows 8时在MSDN杂志上发表的文章。

在文章的最后,他解释了如何设置所需的NavigationService

http://msdn.microsoft.com/en-us/magazine/jj651572.aspx

答案 2 :(得分:2)

MVVMLight中的NavigationService已迁移到名为WinRTBehaviors的新包中。你也可以在Win8nl中获得来自nuget的EventToCommand。看我在这里发布的博客:

入门w / MVVM Light for Windows 8,EventToCommand和Behaviors http://blog.tattoocoder.com/2012/08/getting-started-w-windows-8-mvvm-light.html