我正在使用 MVVM 的 Xamarin Forms。我在日志中得到以下内容:
绑定:"XXX"属性在"YYY"上找不到,目标属性:"Xamarin.Forms.Label.Text">
不确定它是否相关,但是当我在 Command 函数中更新变量时,该变量的更新值不会反映在视图中。 这在我的其他视图模型和视图中没有发生。我不知道为什么。
请帮忙!
这就是我在视图模型中定义变量和在视图中定义绑定的方式。
视图模型
public string _testContextPassing;
public string TestContextPassing
{
get { return _testContextPassing; }
set
{
testContextPassing = value;
OnPropertyChanged();
}
//...
public override async Task Init()
{
TestContextPassing = "123";
}
//...
TestContextPassing = "456";
视图
<Label Grid.Row="2" BindingContext="{Binding Source={x:Reference PhotoCapturePage}, Path=BindingContext}" Text="{Binding TestContextPassing}"/>
<Label Grid.Row="3" Text="{Binding TestContextPassing}"/>
你需要实现 INotifyPropertyChanged 到你的类并执行以下操作
OnPropertyChanged("TestContextPassing");
或
OnPropertyChanged(nameof(TestContextPassing));