UWP Prism ViewModel 为空

UWP Prism ViewModel Is Null

使用 Visual Studio 2017 通用 Windows 模板我使用 Prism 创建了一个测试 UWP 应用程序。一切正常,直到我向应用程序添加了一个新的空白页面。视图名为:

AbcPage

XAML

<Page
x:Class="UI_Test_1.Views.AbcPage"
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:local="using:UI_Test_1.Views"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:prismMvvm="using:Prism.Windows.Mvvm"
prismMvvm:ViewModelLocator.AutoWireViewModel="True"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
mc:Ignorable="d">

<Grid>
    <Button Click="Button_Click" Content="test" />
</Grid>

我添加了

 xmlns:prismMvvm="using:Prism.Windows.Mvvm"
 prismMvvm:ViewModelLocator.AutoWireViewModel="True"

后面的代码是:

 namespace UI_Test_1.Views
{
   public sealed partial class AbcPage : Page
   {
      AbcPageViewModel viewModel => DataContext as AbcPageViewModel;
      public AbcPage()
      {
          this.InitializeComponent();
      }
      private void Button_Click(object sender, RoutedEventArgs e)
      {
         var vm = viewModel;//this is null
       }
  }
}

最后是我的 ViewModel:

namespace UI_Test_1.ViewModels
{
   public class AbcPageViewModel : ViewModelBase
   {
       public AbcPageViewModel()
       {
       //never called
        }
    }
 }

约定似乎是正确的还是我弄错了?为什么

 AbcViewModel

空?我该如何调试?

要在 uwp 中使用 prism 早期版本,您需要基于原生 uwp 项目进行更多配置,例如 App class 和 Page class。当然,官方已经提供了code sample你可以参考一下。

public sealed partial class App : PrismUnityApplication
{
    public App()
    {
        InitializeComponent();
    }

    protected override UIElement CreateShell(Frame rootFrame)
    {
        var shell = Container.Resolve<AppShell>();
        shell.SetContentFrame(rootFrame);
        return shell;
    }

    protected override Task OnInitializeAsync(IActivatedEventArgs args)
    {
        Container.RegisterInstance<IResourceLoader>(new ResourceLoaderAdapter(new ResourceLoader()));
        return base.OnInitializeAsync(args);
    }

    protected override Task OnLaunchApplicationAsync(LaunchActivatedEventArgs args)
    {
        NavigationService.Navigate(PageTokens.Main.ToString(), null);
        return Task.FromResult(true);
    }
}

上次未发布的7.2版本有新的使用方式。有关更多信息,请查看此 link.

sealed partial class App : PrismApplication
{
    public static IPlatformNavigationService NavigationService { get; private set; }

    public App()
    {
        InitializeComponent();
    }

    public override void RegisterTypes(IContainerRegistry container)
    {
        container.RegisterForNavigation<MainPage, MainPageViewModel>(nameof(Views.MainPage));
    }

    public override void OnInitialized()
    {
        NavigationService = Prism.Navigation.NavigationService
            .Create(new Frame(), Gestures.Back, Gestures.Forward, Gestures.Refresh);
        NavigationService.SetAsWindowContent(Window.Current, true);
    }

    public override void OnStart(StartArgs args)
    {
        NavigationService.NavigateAsync(nameof(Views.MainPage));
    }
}

我的命名约定有误。如果您的页面是 AbcPage,那么视图模型应该是 AbcViewModel 而不是 AbcPageViewModel。