【发布时间】:2017-04-24 22:58:37
【问题描述】:
我正在使用最新的 Template 10 VS 扩展来创建 UWP Windows 10 移动应用。
我已经更新了模板以使用 IOC (Autofac),因此 ViewModels 在 app.xaml.cs 覆盖 INavigable ResolveForPage(Page page, NavigationService) 方法中得到解决。
我还更新了 Page 类,每个类都有一个 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,并且绑定到视图模型有效。由于我安装了模板 10 验证包,我更新了一些视图以使用旧的 Binding 方法,例如
<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}" 不适用于错误Cannot resolve symbol ViewModel due to unknown DataContext。
由于我是 UWP 的新手,我想我缺少一些必需的配置来确保 DataContext 设置为正确的 ViewModel。我确实尝试在app.xaml.cs 构造函数中添加DataContext = this,但这不起作用。
谁能告诉我我错过了谜题的哪一部分?
【问题讨论】:
标签: xaml uwp datacontext template10