编程语言
首页 > 编程语言> > c# – UWP:BrightnessOverride StartOverride逻辑

c# – UWP:BrightnessOverride StartOverride逻辑

作者:互联网

有人知道StartOverride方法逻辑吗?来自文档:

The request to start overriding does not mean that the screen
brightness level always changes to match the specified override
brightness level. It’s still up to the system to decide whether or not
to honor this override request.

我编写了简单的方法来调试亮度变化:

protected override void ApplyBrightness(double brightness)
{
    var brightnessOverride = BrightnessOverride.GetForCurrentView();
    Debug.WriteLine($"Try to change brightness from {brightnessOverride.BrightnessLevel} to {brightness}");

    void overrideActiveChangedHandler(BrightnessOverride sender, object e)
    {
        sender.IsOverrideActiveChanged -= overrideActiveChangedHandler;
        Debug.WriteLine($"BrightnessOverride IsOverrideActive changed to {sender.IsOverrideActive}");
    }

    void brightnessLevelChangedHandler(BrightnessOverride sender, object e)
    {
        sender.BrightnessLevelChanged -= brightnessLevelChangedHandler;
        Debug.WriteLine($"BrightnessOverride BrightnessLevel changed to {sender.BrightnessLevel}");
    }

    brightnessOverride.IsOverrideActiveChanged += overrideActiveChangedHandler;
    brightnessOverride.BrightnessLevelChanged += brightnessLevelChangedHandler;

    brightnessOverride.SetBrightnessLevel(brightness, DisplayBrightnessOverrideOptions.None);

    brightnessOverride.StartOverride();
}

得到以下结果:

Try to change brightness from 0.83 to 0.23
BrightnessOverride IsOverrideActive changed to True
BrightnessOverride BrightnessLevel changed to 0.23
Try to change brightness from 0.23 to 0.53
BrightnessOverride IsOverrideActive changed to True
BrightnessOverride BrightnessLevel changed to 0.53
Try to change brightness from 0.53 to 0.14
Try to change brightness from 0.53 to 0.25
Try to change brightness from 0.53 to 0.7
BrightnessOverride IsOverrideActive changed to True
BrightnessOverride BrightnessLevel changed to 0.7

从该测试我可以看出,对于第一次亮度变化,可以降低亮度(0.83-> 0.23),但是在亮度之后可以仅增加亮度(0.23-> 0.53,0.53-> 0.7).随后尝试降低它是失败的:0.53 – > 0.14,0.53 – > 0.25

有没有人对这种奇怪的行为有解释?也许我错过了什么?是否有100%强大的解决方案来控制应用亮度?

我的系统:Windows 10 Pro,版本1709,操作系统版本16299.125.
应用程序目标和最小版本是Windows 10 Creators Update(10.0; Build 15063)

解决方法:

我通过实验发现,BrightnessOverride.GetForCurrentView()每次调用时都会返回一个新的BrightnessOverride实例.这方面的一个证据是,即使每次调用方法时都向实例添加新的事件处理程序,输出也只出现一次.

这意味着,当您多次调用该方法时,您将创建多个BrightnessOverride实例,每个实例都有自己的值.我认为,因为API通常用于在有人从屏幕上扫描优惠券代码时使屏幕更亮,当更低的亮度覆盖在另一个高亮度覆盖激活时,它会被忽略(以保持尊重高亮度请求) ).

如果要将亮度更改为任何值,则只需确保重新使用从GetForCurrentView调用中检索到的第一个实例,因为通过在该实例上设置新亮度,您将“取消”原始请求.

private BrightnessOverride brightnessOverride = null;

private void InitializeBrightnessOverride()
{
    brightnessOverride = BrightnessOverride.GetForCurrentView();
    Debug.WriteLine($"Try to change brightness from {brightnessOverride.BrightnessLevel} to {brightness}");

    void overrideActiveChangedHandler(BrightnessOverride sender, object e)
    {
        sender.IsOverrideActiveChanged -= overrideActiveChangedHandler;
        Debug.WriteLine($"BrightnessOverride IsOverrideActive changed to {sender.IsOverrideActive}");
    }

    void brightnessLevelChangedHandler(BrightnessOverride sender, object e)
    {
        sender.BrightnessLevelChanged -= brightnessLevelChangedHandler;
        Debug.WriteLine($"BrightnessOverride BrightnessLevel changed to {sender.BrightnessLevel}");
    }

    brightnessOverride.IsOverrideActiveChanged += overrideActiveChangedHandler;
    brightnessOverride.BrightnessLevelChanged += brightnessLevelChangedHandler;
}

protected override void ApplyBrightness(double brightness)
{
    brightnessOverride.SetBrightnessLevel(brightness, DisplayBrightnessOverrideOptions.None);

    brightnessOverride.StartOverride();
}

现在你可以打电话:

InitializeBrightnessOverride();
ApplyBrightness(0.7);
ApplyBrightness(0.14);

重新使用该实例就足以使这项工作成功.另外作为提示 – 将屏幕恢复为原始亮度,您可以调用StopOverride.

标签:c,uwp,windows-10-universal
来源: https://codeday.me/bug/20190627/1305011.html