问题
Sir, I am developing an application for a device running WINDOWS CE 5.0. I want to know the battery/power information of the device. I am quit new in CE programming. Please make clear is there any difference between windows mobile programming and CE programming? I found MICROSOFT.WINDOWSMOBILE.STATUS namespace to work with. But when i include the reference, this namespace is not visible in the list. What should i do?
回答1:
You need to P/Invoke GetSystemPowerStatusEx. An example is actually found in this old MSDN article.
回答2:
I have used the P/Invoke GetSystemPowerStatusEx before and found it caused memory leaks when calling it frequently. For reliable event driven battery status I'd go down the route of SystemState Events.
using Microsoft.WindowsMobile.Status;
_batteryState = new SystemState(SystemProperty.PowerBatteryState) { ComparisonType = StatusComparisonType.AnyChange };
_batteryState.Changed += (o, e) => UpdateBatteryIcon();
private void UpdateBatteryIcon()
{
var batteryLevel = SystemState.PowerBatteryStrength;
var isOnCharge = IsOnCharge(SystemState.PowerBatteryState);
_batteryWarning = batteryLevel == BatteryLevel.VeryLow && !isOnCharge;
pictBattery.Image = GetBatteryIcon(batteryLevel, isOnCharge);
}
来源:https://stackoverflow.com/questions/5415622/getting-battery-information-of-a-windows-ce-5-0-device