Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 729
Siva Sankar Gorantla Avatar asked Dec 08 '25 18:12

Siva Sankar Gorantla


2 Answers

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

like image 172
Haukinger Avatar answered Dec 10 '25 13:12

Haukinger


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

like image 39
Muhammad asif Avatar answered Dec 10 '25 14:12

Muhammad asif



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!