Windows phone 8 GetSystemPowerStatus for windows phone 8

Windows phone 8 GetSystemPowerStatus for windows phone 8,windows-phone-8,Windows Phone 8,我在windows phone 8 sdk中看到了winbase.h(WINAPI)文件(kernel32.dll),该文件具有以下功能: GetSystemPowerStatus将返回电池 状态(系统\电源\状态) 。 问题是这会在模拟器上抛出异常,而不是在手持设备上测试(等待得到一个) 我用过 [DllImport("Kernel32")] private static extern Boolean GetSystemPowerStatus( SystemPowerStatus sp

我在windows phone 8 sdk中看到了winbase.h(WINAPI)文件(kernel32.dll),该文件具有以下功能:

GetSystemPowerStatus将返回电池 状态(系统\电源\状态)

。 问题是这会在模拟器上抛出异常,而不是在手持设备上测试(等待得到一个)

我用过

 [DllImport("Kernel32")]
   private static extern Boolean GetSystemPowerStatus( SystemPowerStatus sps );
代码符合要求,但在运行时引发异常


任何想法都可以在手机上使用,或者windows phone 8完全不支持此功能?

如果您只想访问电池信息和bool属性,如果手机是否带充电器,您可以使用此功能:

使用Microsoft.Phone.Info;
使用Windows.Phone.Devices.Power;
名称空间核心.Helpers
{
公共级电池助手
{
公共静态int BATTERYLEVEL
{
得到
{
返回电池。GetDefault()。剩余电量百分比;
}
}
公共静电放电
{
得到
{
return DeviceStatus.PowerSource==PowerSource.External;
}
}
}
}

GetSystemPowerStatus不在Windows Phone 8支持的Win32 API列表中:


正如AnderZubi所说,这不是Windows Phone 8上支持的Win32 API。但是,您可以从本机C/C++代码中调用一个等价的代码。这与马丁发布的C#API非常相似

如果你已经在C/C++中使用WiRT版本,可以节省你需要在C++和C语言之间架起桥梁。如果你正在开发一个只使用XAML/C的新应用程序,那么Martin的答案会更简单

例如:

int WindowsPhoneRuntimeComponent::GetBatteryRemainingPercent()
{
    auto battery = Windows::Phone::Devices::Power::Battery::GetDefault();
    int remainingPercent = battery->RemainingChargePercent;
    return remainingPercent;
}

您好,感谢您的快速回复,我已经看到了这个api,它可以正常工作,但它只提供了非常基本的信息,我需要检查电压、电池类型、电池性能(降级或未降级)以及SYSTEM_POWER_STATUS结构中定义的所有参数。是否有任何等效的api来执行此操作?是否有windows phone 8支持的等效api?据我所知,没有。您可以检索的唯一附加信息是,如果手机处于节能模式,请使用PowerManager api: