【问题标题】:GetSystemPowerStatus for windows phone 8Windows Phone 8 的 GetSystemPowerStatus
【发布时间】:2012-11-20 23:54:34
【问题描述】:

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

GetSystemPowerStatus 将返回电池 状态(SYSTEM_POWER_STATUS)

。 问题是模拟器上的这个抛出和异常,没有在手机上测试(等待得到一个)

我用过

 [DllImport("Kernel32")]
   private static extern Boolean GetSystemPowerStatus( SystemPowerStatus sps );

代码符合但在运行时抛出异常。

任何想法都可以在手机上使用,或者 Windows phone 8 根本不支持?

【问题讨论】:

    标签: windows-phone-8


    【解决方案1】:

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

    如果您已经在 C/C++ 中使用 WinRT 版本,您可能无需在 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 可以做到这一点?
    【解决方案2】:

    如果您只想访问电池信息和 bool 属性,无论手机是否在充电器上,您都可以使用:

    using Microsoft.Phone.Info;
    using Windows.Phone.Devices.Power;
    
    namespace Core.Helpers
    {
        public class BatteryHelper
        {
            public static int BateryLevel
            {
                get
                {
                    return Battery.GetDefault().RemainingChargePercent;
                }
            }
    
            public static bool IsCharging
            {
                get
                {
                    return DeviceStatus.PowerSource == PowerSource.External;
                }
            }
        }
    }
    

    【讨论】:

      【解决方案3】:

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

      http://msdn.microsoft.com/en-us/library/windowsphone/develop/jj662956(v=vs.105).aspx

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多