在应用商店应用中使用 MVVM 进行页面导航

本文关键字:应用 导航 MVVM | 更新日期: 2023-09-27 18:29:36

我对这个问题非常头疼。我真的很不喜欢商店应用程序,但在这种情况下被迫使用它。我只使用 XAML 几个星期。

我的问题是:如何在ViewModel中调用一个RelayCommand(当然是从我的观点中(,这将改变我视图上的页面?更好的是,使用 URI 更改它,以便我可以将命令参数传递给文件。

我完全迷失在这一点上。目前,我正在使用后面的视图代码中的this.Frame.Navigate(type type)来浏览页面。

真的,我的意思是真的很感激从 a 到 z 关于在这种情况下该怎么做的描述。

我想我可以做一些事情,比如在我的视图上构建一个框架容器并将其发送到我的视图模型,然后从那里将当前框架导航到另一个框架。但我不确定这在应用商店应用中是如何工作的。

对于缺乏好的问题,我真的很抱歉,但是我有截止日期,我需要以适当的方式将我的视图连接到我的ViewModel。我不喜欢同时拥有视图代码隐藏和视图模型代码。

在应用商店应用中使用 MVVM 进行页面导航

有两种

方法可以做到这一点,一个简单的方法是将中继命令操作从视图传递到视图模型。

public MainPage()
{
  var vm = new MyViewModel();
  vm.GotoPage2Command = new RelayCommand(()=>{ Frame.Navigate(typeof(Page2)) });
  this.DataContext = vm;
}
<Button Command={Binding GoToPage2Command}>Go to Page 2</Button>

另一种方法是使用 IocContainer 和 DependencyInjection。这是一种更失败的耦合方法。

我们将需要一个用于导航页面的界面,这样我们就不需要引用或了解有关PageX或任何UI元素的任何信息,假设您的视图模型位于一个对UI一无所知的单独项目中。

视图模型项目:

  public interface INavigationPage
  {
    Type PageType { get; set; }
  }
  public interface INavigationService
  {
    void Navigate(INavigationPage page) { get; set; }
  }

public class MyViewModel : ViewModelBase
  {
    public MyViewModel(INavigationService navigationService, INavigationPage page)
    {
      GotoPage2Command = new RelayCommand(() => { navigationService.Navigate(page.PageType); })
    }
    private ICommand GotoPage2Command { get; private set; }
  }

用户界面项目:

  public class NavigationService : INavigationService
    {
       //Assuming that you only navigate in the root frame
       Frame navigationFrame = Window.Current.Content as Frame;
       public void Navigate(INavigationPage page)
       {
          navigationFrame.Navigate(page.PageType);
       }
    }
public abstract class NavigationPage<T> : INavigationPage
{
   public NavigationPage()
   {
      this.PageType = typeof(T);
   }
}
public class NavigationPage1 : NavigationPage<Page1> { }

public class MainPage : Page
{
   public MainPage()
   {
      //I'll just place the container logic here, but you can place it in a bootstrapper or in app.xaml.cs if you want. 
      var container = new UnityContainer();
      container.RegisterType<INavigationPage, NavigationPage1>();
      container.RegisterType<INavigationService, NavigationService>();
      container.RegisterType<MyViewModel>();
      this.DataContext = container.Resolve<MyViewModel>();       
   }
}

正如斯科特所说,你可以使用导航服务。我将首先创建一个接口,这在本例中不是必需的,但如果您将来使用依赖注入(具有视图模型和服务的良好解决方案(:)

服务:

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

导航服务.cs将继承INavigationService您将需要以下命名空间

using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

public sealed class NavigationService : INavigationService
{
    public void Navigate(Type sourcePage)
    {
        var frame = (Frame)Window.Current.Content;
        frame.Navigate(sourcePage);
    }
    public void Navigate(Type sourcePage, object parameter)
    {
        var frame = (Frame)Window.Current.Content;
        frame.Navigate(sourcePage, parameter);
    }
    public void GoBack()
    {
        var frame = (Frame)Window.Current.Content;
        frame.GoBack();
    }
}

简单视图模型显示中继命令示例。注意:我使用 DoSomething RelayCommand 导航到另一个页面 (Page2.xaml(。

我的视图模型.cs

public class MyViewModel : INotifyPropertyChanged
{
    private INavigationService _navigationService;
    public event PropertyChangedEventHandler PropertyChanged;
    public void OnPropertyChanged(string propertyName)
    {
        var handler = PropertyChanged;
        if (handler != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    public MyViewModel(INavigationService navigationService)
    {
        _navigationService = navigationService;
    }
    private ICommand _doSomething;
    public ICommand DoSomething
    {
        get
        {
            return _doSomething ??
                new RelayCommand(() =>
                    {
                        _navigationService.Navigate(typeof(Page2));
                    });
        }
    }}

在简单的示例中,我在主页中创建了视图模型.cs并添加了导航服务但您可以在其他地方执行此操作,具体取决于您的 MVVM 设置是什么样的。

主页.cs

public sealed partial class MainPage : Page
{
    public MainPage()
    {
        this.InitializeComponent();
        var vm = new MyViewModel(new NavigationService());
        this.DataContext = vm;
    }
}

MainPage.xaml(绑定到命令DoSomething(

 <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <Button Width="200" Height="50" Content="Go to Page 2"
             Command="{Binding DoSomething}"/>
</Grid>

希望有帮助。

我不太喜欢 ViewModel 引用要导航到的视图。所以我更喜欢视图模型优先的方法。通过使用ContentControls,ViewModel类型的DataTemplates和我的ViewModels中的某种导航模式。

我的导航如下所示:

[ImplementPropertyChanged]
public class MainNavigatableViewModel : NavigatableViewModel
{
    public ICommand LoadProfileCommand { get; private set; }
    public ICommand OpenPostCommand { get; private set; }
    public MainNavigatableViewModel ()
    {
        LoadProfileCommand = new RelayCommand(() => Navigator.Navigate(new ProfileNavigatableViewModel()));
        OpenPostCommand = new RelayCommand(() => Navigator.Navigate(new PostEditViewModel { Post = SelectedPost }), () => SelectedPost != null);
    }
}

我的 NavigatableViewModel 如下所示:

[ImplementPropertyChanged]
public class NavigatableViewModel
{
    public NavigatorViewModel Navigator { get; set; }
    public NavigatableViewModel PreviousViewModel { get; set; }
    public NavigatableViewModel NextViewModel { get; set; }
}

还有我的导航器:

[ImplementPropertyChanged]
public class NavigatorViewModel
{
    public NavigatableViewModel CurrentViewModel { get; set; }
    public ICommand BackCommand { get; private set; }
    public ICommand ForwardCommand { get; private set; }
    public NavigatorViewModel()
    {
        BackCommand = new RelayCommand(() =>
        {
            // Set current control to previous control
            CurrentViewModel = CurrentViewModel.PreviousViewModel;
        }, () => CurrentViewModel != null && CurrentViewModel.PreviousViewModel != null);
        ForwardCommand = new RelayCommand(() =>
        {
            // Set current control to next control
            CurrentViewModel = CurrentViewModel.NextViewModel;
        }, () => CurrentViewModel != null && CurrentViewModel.NextViewModel != null);
    }
    public void Navigate(NavigatableViewModel newViewModel)
    {
        if (newViewModel.Navigator != null && newViewModel.Navigator != this)
            throw new Exception("Viewmodel can't be added to two different navigators");
        newViewModel.Navigator = this;
        if (CurrentViewModel != null)
        {
            CurrentViewModel.NextViewModel = newViewModel;
        }
        newViewModel.PreviousViewModel = CurrentViewModel;
        CurrentViewModel = newViewModel;
    }
}

My MainWindows.xaml:

<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:viewmodels="clr-namespace:MyApp.ViewModels"
        x:Class="MyApp.Windows.MainWindow"
        mc:Ignorable="d"
        Title="MainWindow" Height="389" Width="573" 
        d:DataContext="{d:DesignInstance {x:Type viewmodels:MyAppViewModel}, IsDesignTimeCreatable=True}">
    <Grid>
        <!-- Show data according to data templates as defined in App.xaml -->
        <ContentControl Content="{Binding Navigator.CurrentViewModel}"  Margin="0,32,0,0" />
        <Button Content="Previous" Command="{Binding Navigator.BackCommand}" Style="{DynamicResource ButtonStyle}" HorizontalAlignment="Left" Margin="10,5,0,0" VerticalAlignment="Top" Width="75" />
        <Button Content="Next" Command="{Binding Navigator.ForwardCommand}" Style="{DynamicResource ButtonStyle}" HorizontalAlignment="Left" Margin="90,5,0,0" VerticalAlignment="Top" Width="75" />
    </Grid>
</Window>

App.xaml.cs:

public partial class App
{
    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);
        new MainWindow {DataContext = new MyAppViewModel()}.Show();
    }
}

我的应用视图模型:

[ImplementPropertyChanged]
public class MyAppViewModel
{
    public NavigatorViewModel Navigator { get; set; }
    public MyAppViewModel()
    {
        Navigator = new NavigatorViewModel();
        Navigator.Navigate(new MainNavigatableViewModel());
    }
}

App.xaml:

        <DataTemplate DataType="{x:Type viewmodels:MainNavigatableViewModel}">
            <controls:MainControl/>
        </DataTemplate>
        <DataTemplate DataType="{x:Type viewmodels:PostEditViewModel}">
            <controls:PostEditControl/>
        </DataTemplate>

缺点是你有更多的 ViewModel 代码来管理你正在查看的内容的状态。但显然,就可测试性而言,这也是一个巨大的优势。当然,您的视图模型不需要依赖于您的视图。

另外,我使用Fody/PropertyChanged,这就是[ImplementPropertyChanged]的意义所在。阻止我编写 OnPropertyChanged 代码。

这是实现导航服务的另一种方法,无需使用抽象类,也不引用视图模型中的视图类型。

假设目标页面的视图模型如下所示:

public interface IDestinationViewModel { /* Interface of destination vm here */ }
class MyDestinationViewModel : IDestinationViewModel { /* Implementation of vm here */ }

然后,导航服务可以简单地实现以下接口:

public interface IPageNavigationService
{
    void NavigateToDestinationPage(IDestinationViewModel dataContext);
}

在主窗口 ViewModel 中,您需要注入目标页面的导航器和视图模型:

class MyViewModel1 : IMyViewModel
{
    public MyViewModel1(IPageNavigationService navigator, IDestinationViewModel destination)
    {
        GoToPageCommand = new RelayCommand(() => 
                navigator.NavigateToDestinationPage(destination));
    }
    public ICommand GoToPageCommand { get; }
}

导航服务的实现封装了视图类型 (Page2( 和对通过构造函数注入的框架的引用:

class PageNavigationService : IPageNavigationService
{
    private readonly Frame _navigationFrame;
    public PageNavigationService(Frame navigationFrame)
    {
        _navigationFrame = navigationFrame;
    }
    void Navigate(Type type, object dataContext)
    {
        _navigationFrame.Navigate(type);
        _navigationFrame.DataContext = dataContext;
    }
    public void NavigateToDestinationPage(IDestinationViewModel dataContext)
    {
        // Page2 is the corresponding view of the destination view model
        Navigate(typeof(Page2), dataContext);
    }
}

要获取框架,只需在主页 xaml 中命名它:

<Frame x:Name="RootFrame"/>

在 MainPage 的代码隐藏中,通过传递根帧来初始化引导程序:

public sealed partial class MainPage : Page
{
    public MainPage()
    {
        this.InitializeComponent();
        var bootstrapper = new Bootstrapper(RootFrame);
        DataContext = bootstrapper.GetMainScreenViewModel();
    }
}

最后,这里是引导程序实现完整性;)

class Bootstrapper
{
    private Container _container = new Container();
    public Bootstrapper(Frame frame)
    {
        _container.RegisterSingleton(frame);
        _container.RegisterSingleton<IPageNavigationService, PageNavigationService>();
        _container.Register<IMyViewModel, MyViewModel1>();
        _container.Register<IDestinationViewModel, IDestinationViewModel>();
#if DEBUG
        _container.Verify();
#endif
    }
    public IMyViewModel GetMainScreenViewModel()
    {
        return _container.GetInstance<IMyViewModel>();
    }
}

这让我感到困扰,因为没有人在架构层面解决这个问题。因此,这是使用内置的基于框架的导航完全解耦视图,视图模型以及它们之间的映射的代码。该实现使用 Autofact 作为 DI 容器,但可以轻松移植到其他 IoC 解决方案。

核心 VM 逻辑(这些逻辑应位于同一程序集中(:

// I would not get into how the ViewModel or property change notification is implemented
public abstract class PageViewModel : ViewModel
{
    protected internal INavigationService Navigation { get; internal set; }
    internal void NavigationCompleted()
    {
        OnNavigationCompleted();
    }
    protected virtual void OnNavigationCompleted()
    {
    }
}
public interface INavigationService
{
    void Navigate<TModel>() where TModel : PageViewModel;
}
public abstract class NavigationServiceBase : INavigationService
{
    public abstract void Navigate<TModel>() where TModel : PageViewModel;
    protected void CompleteNavigation(PageViewModel model)
    {
        model.Navigation = this;
        model.NavigationCompleted();
    }
}

此代码应位于 UWP 类库或可执行文件中:

public interface INavigationMap<TModel>
    where TModel: PageViewModel
{
    Type ViewType { get; }
}
internal class NavigationMap<TModel, TView> : INavigationMap<TModel>
    where TModel: PageViewModel
    where TView: Page
{
    public Type ViewType => typeof(TView);
}
public class NavigationService : NavigationServiceBase
{
    private readonly Frame NavigationFrame;
    private readonly ILifetimeScope Resolver;
    public NavigationService(ILifetimeScope scope)
    {
        Resolver = scope;
        NavigationFrame = Window.Current.Content as Frame;
        NavigationFrame.Navigated += NavigationFrame_Navigated;
    }
    private void NavigationFrame_Navigated(object sender, Windows.UI.Xaml.Navigation.NavigationEventArgs e)
    {
        if(e.Content is FrameworkElement element)
        {
            element.DataContext = e.Parameter;
            if(e.Parameter is PageViewModel page)
            {
                CompleteNavigation(page);
            }
        }
    }
    public override void Navigate<TModel>()
    {
        var model = Resolver.Resolve<TModel>();
        var map = Resolver.Resolve<INavigationMap<TModel>>();
        NavigationFrame.Navigate(map.ViewType, model);
    }
}

其余的只是在 DI 和使用示例中注册的便利代码:

public static class NavigationMap
{
    public static void RegisterNavigation<TModel, TView>(this ContainerBuilder builder)
        where TModel : PageViewModel
        where TView : Page
    {
        builder.RegisterInstance(new NavigationMap<TModel, TView>())
            .As<INavigationMap<TModel>>()
            .SingleInstance();
    }
}
     builder.RegisterNavigation<MyViewModel, MyView>();

public class UserAuthenticationModel : PageViewModel
{
    protected override void OnNavigationCompleted()
    {
        // UI is visible and ready
        // navigate to somewhere else
        Navigation.Navigate<MyNextViewModel>();
    }
}