3

我正在尝试在我的应用程序中实现 ViewModelLocator 模式,但 ViewModel 没有连接到视图。我是 prism 的新手,如果我在这里遗漏任何东西,请告诉我。

看法:

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

视图模型:

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

文件夹结构:

在此处输入图像描述

4

2 回答 2

5

确保视图位于Views命名空间中,视图模型ViewModels分别位于 中。

于 2017-09-12T09:45:05.533 回答
1

在 xaml 文件和 ViewModels 文件夹中将 WpfApp1.MainWindow 更改为 WpfApp1.Views.MainWindow 更改命名空间 wpfApp1.ViewModels。这对我有用。

于 2018-10-14T05:17:08.000 回答