Home > C#, Windows Phone > How to use the new PowerSavingModeEnabled property via reflection in Windows Phone 8 Update 3

How to use the new PowerSavingModeEnabled property via reflection in Windows Phone 8 Update 3

21/10/2013

Windows Phone 8 Update 3 includes some new or changed features for app developers, as we can read on MSDN. Among the other things, a new boolean property, Windows.Phone.System.Power.PowerManager.PowerSavingModeEnabled, has been added. It indicates whether battery saving mode is turned on. Because there isn’t a corresponding Windows Phone SDK for this update, we need to use reflection to access this property.

So, we can create an helper class like this:

public static class PowerManager2
{
    private static Version TargetedVersion = new Version(8, 0, 10492);
    private static PropertyInfo powerSavingModeEnabledProperty;

    static PowerManager2()
    {
        var isTargetedVersion = Environment.OSVersion.Version >= TargetedVersion;
        if (isTargetedVersion)
        {
            var powerManagerType = typeof(Windows.Phone.System.Power.PowerManager);
            powerSavingModeEnabledProperty = 
                powerManagerType.GetProperty("PowerSavingModeEnabled");
        }
    }

    public static bool? PowerSavingModeEnabled
    {
        get
        {
            if (powerSavingModeEnabledProperty != null)
            {
                var powerSavingModeEnabled = 
                    (bool)powerSavingModeEnabledProperty.GetValue(null);
                return powerSavingModeEnabled;
            }

            return null;
        }
    }
}

First of all, in the constructor of PowerManager2 we check the OS version to test whether the device has Windows Phone 8 Update 3. In this case, we retrieve the PowerSavingModeEnabled property through reflection.

Then, in our PowerSavingModeEnabled property implementation, we retrieve its value. If Update 3 isn’t installed, we simply return null.

Categories: C#, Windows Phone
Comments are closed.
%d bloggers like this: