Xaml绑定无法在模板10应用程序中查看ViewModel

Xaml绑定无法在模板10应用程序中查看ViewModel,xaml,uwp,datacontext,template10,Xaml,Uwp,Datacontext,Template10,我正在使用最新的Template 10 VS扩展创建一个UWP Windows 10移动应用程序。 我已经更新了模板以使用IOC(Autofac),以便在app.xaml.cs覆盖不可用的ResolveForPage(Page Page,NavigationService)方法中解析ViewModels。 我还更新了页面类,使每个类都具有ViewModel属性,例如: public sealed partial class LoginPage : Page { private Login

我正在使用最新的Template 10 VS扩展创建一个UWP Windows 10移动应用程序。 我已经更新了模板以使用IOC(Autofac),以便在
app.xaml.cs
覆盖
不可用的ResolveForPage(Page Page,NavigationService)
方法中解析ViewModels。 我还更新了页面类,使每个类都具有ViewModel属性,例如:

public sealed partial class LoginPage : Page
{
    private LoginPageViewModel _viewModel;

    public LoginPageViewModel ViewModel => _viewModel ?? (_viewModel = (LoginPageViewModel)DataContext);

    public LoginPage()
    {
        InitializeComponent();
        NavigationCacheMode = Windows.UI.Xaml.Navigation.NavigationCacheMode.Enabled;
    }
}
到目前为止,这还不错,因为我只在视图中使用了
x:Bind
,并且绑定到viewmodel是有效的。自从我安装了Template 10验证包之后,我更新了一些视图,以使用较旧的
绑定方法,例如

<validate:ControlWrapper PropertyName="Password">
            <TextBox x:Name="Password" 
                 HorizontalAlignment="Left"
                 Margin="10,220,0,0" 
                 TextWrapping="Wrap"
                 Text="{Binding ViewModel.LoginModel.Password, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
                 VerticalAlignment="Top"
                 Width="{StaticResource FieldWidth}"
                 Height="60" 
                 PlaceholderText="Password" 
                 FontSize="24" 
                 InputScope="Password">
                <Interactivity:Interaction.Behaviors>
                    <Core:EventTriggerBehavior>
                        <Behaviors:FocusAction />
                    </Core:EventTriggerBehavior>
                </Interactivity:Interaction.Behaviors>
            </TextBox>
        </validate:ControlWrapper>

我遇到的这个问题是,文本绑定,
text=“{binding ViewModel.LoginModel.Password,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}”
无法处理错误
由于未知数据上下文而无法解析符号ViewModel

由于我是UWP新手,我认为我缺少一些必需的配置,以确保DataContext设置为正确的ViewModel。我确实尝试过在
app.xaml.cs
构造函数中添加
DataContext=this
,但没有成功


有人能告诉我我遗漏了拼图的哪一部分吗?

在这里检查新的x:Bind和旧的绑定之间的区别。根据错误消息,旧绑定正在页面的DataContext上查找名为“ViewModel”的属性。但是DataContext的类型为“LoginPageViewModel”,属性为“LoginModel”?如果我是对的,您需要将文本绑定更改为

Text="{Binding LoginModel.Password, Mode=...
我认为这应该是一个很好的开端,可以引导你走向正确的方向;)


学习和理解新旧绑定之间的差异也很有帮助:

这正是我需要的提示,让它工作起来。按照您的建议设置绑定是有效的。我也会更透彻地读一读。现在,了解为什么在代码中设置的验证错误没有显示在视图中。谢谢你的帮助。@Steve很高兴它有帮助。问题通常是阅读、学习和深入了解细节的良好开端,以了解发生了什么以及为什么我没有工作。