我需要检测设备是否安装在底座上(例如,将设备放到充电器支架上)。
下面的代码在大多数情况下都可以工作,但当电池已满并且调用了CradleMonitorService(构造函数时,它就不能工作了。
public class CradleMonitorService
{
public CradleMonitorService()
{
IsCradled = Battery.State == BatteryState.Charging;
Battery.BatteryInfoChanged += Battery_BatteryInfoChanged;
}
public bool IsCradled { get; private set; }
private void Battery_BatteryInfoChanged(object sender, BatteryInfoChangedEventArgs e)
{
IsCradled = (e.PowerSource == BatteryPowerSource.AC || e.PowerSource == BatteryPowerSource.Usb);
}
}有什么解决方案吗?
发布于 2020-01-06 02:53:44
请改用Battery.PowerSource进行检测。
public CradleMonitorService()
{
IsCradled = Battery.PowerSource == BatteryPowerSource.AC || Battery.PowerSource == BatteryPowerSource.Usb
Battery.BatteryInfoChanged += Battery_BatteryInfoChanged;
}https://stackoverflow.com/questions/59584017
复制相似问题