C# 装订不';t更新属性更改
我有一个viewmodel,其中我定期从蓝牙模块更新我的一个参数。(我的setter中有一个断点,因此我确信它正在更新) 参数在我的viewmodel中确实正确更新,并且我确信我的绑定在我的视图中是正确的 我怀疑我的C# 装订不';t更新属性更改,c#,xamarin,C#,Xamarin,我有一个viewmodel,其中我定期从蓝牙模块更新我的一个参数。(我的setter中有一个断点,因此我确信它正在更新) 参数在我的viewmodel中确实正确更新,并且我确信我的绑定在我的视图中是正确的 我怀疑我的propertyChanged?方法是使我的代码出错的方法: public class Viewmodel : INotifyPropertyChanged { public Viewmodel() { } public strin
propertyChanged?
方法是使我的代码出错的方法:
public class Viewmodel : INotifyPropertyChanged
{
public Viewmodel()
{
}
public string CurrentValue
{
get
{
return _currentValue;
}
set
{
if (_currentValue!= value)
{
_currentValue= value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(CurrentValue));
}
}
}
}
视图:
<Label Text="{Binding BindingContext.CurrentValue, Source={x:Reference Name=MyCarousel}}"/>
仅供参考,我正在使用Xabre-BLE来促进连接,我的特性侦听器更新了我的viewmodel,如下所示:
public async void GetValuesFromCharacteristic()
{
Viewmodel viewModel = new Viewmodel();
Characteristic.ValueUpdated += (s, a) =>
{
Console.WriteLine(Characteristic.Value[7].ToString());
Xamarin.Forms.Device.BeginInvokeOnMainThread(() =>
{
viewModel.CurrentValue = Characteristic.Value[7].ToString();
});
};
await Characteristic.StartUpdatesAsync();
}
在我看来,PropertyChanged?
仍然为空,因此没有更新任何内容 如所述,PropertyChangedEventArgs
的参数应该是一个带有已更改属性名称的字符串
ViewModel还需要实现INotifyPropertyChanged
。示例代码中没有,但我假设这只是这个问题的一个示例定义。如所述,PropertyChangedEventArgs
的参数应该是一个带有已更改属性名称的字符串
ViewModel还需要实现INotifyPropertyChanged
。在您的示例代码中没有,但我假设这只是这个问题的一个示例定义。newpropertychangedeventargs(nameof(CurrentValue))
是解决方案o:PropertyChanged?.Invoke(这是新的PropertyChangedEventArgs(nameof(CurrentValue))代码>它实际上不起作用:-(你能提供一个小样本吗?你的意思是说样本?你可以试着把Viewmodel Viewmodel=newviewmodel();
放在GetValuesFromCharacteristic
方法之外。newpropertychangedeventargs(nameof(CurrentValue))
是解决方案o:PropertyChanged?.Invoke(这个,新的PropertyChangedEventArgs(nameof(CurrentValue));
它实际上不起作用。:-(你能提供一个小样本吗?你的意思是样本吗?你可以试着把Viewmodel-Viewmodel=new-Viewmodel()
超出GetValuesFromCharacteristic
方法的范围。这不起作用。我已经对上面作为注释发布的内容进行了更改。仍然没有任何更改。我认为有一个问题仍然没有解决。这不起作用。我已经对上面作为注释发布的内容进行了更改。仍然没有任何更改。有一个问题仍然没有解决想想看。
public async void GetValuesFromCharacteristic()
{
Viewmodel viewModel = new Viewmodel();
Characteristic.ValueUpdated += (s, a) =>
{
Console.WriteLine(Characteristic.Value[7].ToString());
Xamarin.Forms.Device.BeginInvokeOnMainThread(() =>
{
viewModel.CurrentValue = Characteristic.Value[7].ToString();
});
};
await Characteristic.StartUpdatesAsync();
}