ViewModelLocator in Prism 6 not working

2.6k views Asked by At

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

2

There are 2 answers

0
Haukinger On BEST ANSWER

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

0
Muhammad asif On

Change WpfApp1.MainWindow to WpfApp1.Views.MainWindow in xaml file and in ViewModels folder change namespace wpfApp1.ViewModels. This worked for me.