Prism 6中的ViewModelLocator无法工作

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); }
    }
}

文件夹结构:

enter image description here


可能是Prism 6.1 ViewModelLocator无法实例化我的ViewModel的重复问题。 - Haukinger
你的命名空间是什么?ViewModelLocator 的约定配置是什么? - Haukinger
<Window x:Class="WpfApp1.MainWindow" 看起来 MainWindow 不在 WpfApp1.Views 命名空间中... - Haukinger
2个回答

5

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


1

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接