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?