C# 从蓝牙特性更新viewmodel中的值
我使用Xamarin studio的Xabre-BLE库。在我的项目中,我有一个bluetooth类和一个ViewModel 当我的应用程序连接到指定的特征时,我订阅一个C# 从蓝牙特性更新viewmodel中的值,c#,xamarin,bluetooth-lowenergy,C#,Xamarin,Bluetooth Lowenergy,我使用Xamarin studio的Xabre-BLE库。在我的项目中,我有一个bluetooth类和一个ViewModel 当我的应用程序连接到指定的特征时,我订阅一个characteristic.ValueUpdated事件,从中更新viewmodel中的值 在我的viewmodel中,我有一个propertychanged eventhandler,它监听bluetooth类的更新 但是,由于某些原因,setter没有更新我的值 蓝牙类: public class Bluetooth {
characteristic.ValueUpdated
事件,从中更新viewmodel中的值
在我的viewmodel中,我有一个propertychanged eventhandler,它监听bluetooth类的更新
但是,由于某些原因,setter没有更新我的值
蓝牙类:
public class Bluetooth
{
//code to respectively connect and disconnect
public async void GetValuesFromCharacteristic()
{
CarouselViewModel viewModel = new CarouselViewModel();
Characteristic.ValueUpdated += (s, a) =>
{
viewModel.CurrentValue = Characteristic.Value[7].ToString();
};
await Characteristic.StartUpdatesAsync();
}
}
视图模型:
public class CarouselViewModel
{
private _currentValue;
public CarouselViewModel()
{
}
public string CurrentValue
{
get
{
return _currentValue;
}
set
{
if(_currentValue != value)
{
_currentValue = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(_currentValue));
}
}
}
}
您的Bluetooth类正在创建ViewModel的实例。页面是否也有自己的实例?如果您有多个VM实例,对其中一个的更改将不会反映在其他实例中。其次,您是否在调试器中跟踪了执行情况,以验证您的事件和设置程序是否实际被调用?页面也有一个实例是。但我的setter实际上正在更新!但问题是,当在setter中放置断点时,我的应用程序会中断并告诉我“没有兼容的代码正在运行”-“选定的调试引擎不支持在当前线程上执行任何代码(例如,只有本机运行时代码正在执行)”我假设您的根本问题是您有多个VM副本,并且没有更新“正确”的副本。与其在bluetooth类中构建我的viewmodel,您是否建议我做些其他事情?我建议您的BT类在接收到感兴趣的数据时引发事件或消息。然后,您的虚拟机可以订阅这些事件/消息。