2012-12-03 111 views
1

我看到其中所具有的功能的Windows Phone 8 SDK的WINBASE.H(WINAPI)文件(KERNEL32.DLL):GetSystemPowerStatus Windows Phone的8

GetSystemPowerStatus將返回電池 狀態(SYSTEM_POWER_STATUS)

。 的問題是,這將引發在模擬器上,在手機上沒有測試和異常(等待獲得一個)

我用

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

的代碼是否符合,但在運行時拋出異常。

任何想法將這項工作在手機上,或者這完全不支持Windows Phone 8?

回答

1

如果你只是想訪問電池信息和布爾屬性,如果手機是在充電器或沒有,你可以使用這個:

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; 
      } 
     } 
    } 
} 
0

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

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

+0

是否有任何相當於API,它的Windows Phone 8個支持? – Viral

+0

據我所知,沒有。您可以回收的唯一附加信息是手機處於省電模式,使用PowerManager API: http://msdn.microsoft.com/en-us/library/windowsphone/develop/windows.phone.system.power .powermanager(v = vs.105).aspx – anderZubi

2

正如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; 
} 
+0

嗨,感謝您的快速回復,我已經看到了這個API,它的工作原理,但它只是提供非常基本的信息,我需要檢查電壓,電池類型,電池性能是否降級)以及在SYSTEM_POWER_STATUS結構中定義的所有參數。有沒有相同的API來做到這一點? – Viral