【问题标题】:UWP Prism ViewModel Is NullUWP Prism ViewModel 为空
【发布时间】:2018-11-14 20:25:11
【问题描述】:

使用 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

空?我该如何调试?

【问题讨论】:

  • 您需要将 AbcPageViewModel 添加到 ViewModels 文件夹中,这是 prism 默认尝试搜索的位置。
  • AbcPageViewModel 位于 ViewModels 文件夹中。
  • 你用的是哪个棱镜版本?
  • 唉,我的项目中没有这样的代码。 App.xaml.cs 具有初始化例程,您可以为 Ioc 配置容器,但与链接不同。谢谢。
  • 那么,emm,问题解决了吗?

标签: uwp prism


【解决方案1】:

要在 uwp 中使用 prism 早期版本,您需要在本机 uwp 项目的基础上配置更多配置,例如 App 类和 Page 类。当然,官方已经提供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));
    }
}

【讨论】:

    【解决方案2】:

    我在命名约定上犯了一个错误。如果您的页面是 AbcPage,那么视图模型应该是 AbcViewModel 而不是 AbcPageViewModel。

    【讨论】:

      猜你喜欢
      • 2019-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-05
      • 1970-01-01
      • 2018-04-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多