0
votes

Using the Visual Studio 2017 Universal Windows Templates I created a test UWP App using Prism. It all works fine untill I added a new blank page to the app. The view is called:

AbcPage

The 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>

I added the

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

The code behind is thus:

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

And finally my ViewModel:

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

The conventions appear to be correct or have I made a mistake? Why is the

 AbcViewModel

null? How do I debug this?

2
You need to add your AbcPageViewModel in ViewModels folder, that's where prism will try to search by default. - Dishant
The AbcPageViewModel is in the ViewModels folder. - Paul Stanley
Which prism version you that you used? - Nico Zhu - MSFT
Alas there is no code such as that in my project. App.xaml.cs has initialisation routines and you can configure containers for Ioc but nothing like the link.Thanks. - Paul Stanley
So, emm, the issue solved? - Nico Zhu - MSFT

2 Answers

1
votes

For using prism early releases within uwp, you need config more ting base on native uwp project, such as App class and Page class. Certainly, the official has provide code sample you could refer.

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

For last 7.2 version that does not release has new usage mode. For more please check this 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));
    }
}
1
votes

I made a mistake with the naming conventions. If your page is AbcPage then the viewmodel should be AbcViewModel NOT AbcPageViewModel.