Search code examples
c#wpfmvvmprismprism-6

ViewModelLocator in Prism 6 not working


I am trying to implement ViewModelLocator pattern in my application, but ViewModel is not hooked up to the view. I am new to prism, please let me know if I am missing anything here.

View:

<Window x:Class="WpfApp1.MainWindow"
        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:local="clr-namespace:WpfApp1"
        xmlns:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Wpf"
        prism:ViewModelLocator.AutoWireViewModel="True"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <TextBlock Text="{Binding Message}" FontSize="36" />
    </Grid>
</Window>

ViewModel:

class MainWindowViewModel : BindableBase
{
    string _message = "Hello World";
    public string Message
    {
        get { return _message; }
        set { SetProperty<string>(ref _message, value); }
    }
}

Folder Structure:

enter image description here


Solution

  • Make sure that the views reside in a Views namespace, and the viewmodels in ViewModels, respectively.