0

主窗口用户界面

<Window x:Class="PrismSanity.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:prism="http://prismlibrary.com/"
        prism:ViewModelLocator.AutoWireViewModel="True"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>

        <TextBlock Text="{Binding MyName}"></TextBlock>


    </Grid>
</Window>

主窗口视图模型

public class MainWindowViewModel : BindableBase
    {

        private string _MyName = "John";

        public string MyName
        {
            get { return _MyName = "John"; }
            set { SetProperty(ref _MyName, value); }
        }


    }

我安装了 SNOOP 以查看发生了什么,但 VM 没有链接到视图。如果我用第二个视图做同样的事情,并使用

<ContectContainer>
<views:ViewA/>
</ContentContainer>

在主窗口中然后我让 ViewA 和 ViewAViewModel 链接正常。感谢您的关注。

4

1 回答 1

2

Prism 仅支持UserControl类而不支持Window类。AutoWireViewModel只在UserControl课堂上工作。

我有同样的问题,我在后面的代码中解决了这个绑定上下文 - 部分使用 Unity。像这样的东西:

public partial class ShellView : Window
{
    public ShellView(ShellViewModel viewModel)
    {
        this.InitializeComponent();

        // Set the ViewModel as this View's data context.
        this.DataContext = viewModel;
    }

当应用程序创建 ShellView(它是您的 PrismSanity.MainWindow)时,统一注入 viewModel ( ShellViewModel)

这是很少的技术债务。

于 2016-02-01T07:59:51.643 回答