【问题标题】:UWP v10240 GetAccentColorUWP v10240 获取AccentColor
【发布时间】:2023-03-24 06:47:01
【问题描述】:

我正在尝试使用以下代码在 UWP 平台上获取当前的 UI-AccentColor:

var uiSettings = new UISettings();
var accentColor = uiSettings.GetColorValue(UIColorType.Accent);

此代码适用于 v10586 和 v14939,但不适用于 v10240,但以下异常:

Unable to cast object of 
type 'Windows.UI.ViewManagement.UISettings' to 
type 'Windows.UI.ViewManagement.IUISettings3'.

问题:为什么此代码不适用于 v10240,尽管该方法已在使用的 ApiContract [Assembly Windows.Foundation.UniversalApiContract, Version=1.0.0.0] 中定义,而且所有 AccentColor-EnumValues 都是也在 ApiContract v1 中定义?尽管文档没有提示此类异常,但避免此类错误的最佳做法是什么?

文档指定了 v10240 的使用方法的可用性:UISettings::GetColorValue 和使用的枚举:UIColorType

我已经找到 StackOverflow Get Variations of Accent color in UWP 但这并不能解决我的问题。

展示项目在:https://github.com/janjaali/UwpGetAccentColorVs10240

【问题讨论】:

    标签: c# uwp


    【解决方案1】:

    这看起来像是合同清单中的一个错误,然后流入了文档中。

    GetColorValue 位于 IUISettings3 中(因此提到了铸造错误),这是 11 月更新(内部版本 10586)的新内容。应该是 UniversalApiContract 2.0 版

    您应该能够通过 ApiInformation 类检查 API 是否可用。这将创建一个带有 Accent 颜色的画笔,或者如果 GetColorValue 不存在,则回退到应用特定的默认值:

    Brush accentBrush = (Brush) App.Current.Resources["CustomAppAccentBrush"];
    if (Windows.Foundation.Metadata.ApiInformation.IsMethodPresent("Windows.UI.ViewManagement.UISettings","GetColorValue"))
    {
        var UISettings = new Windows.UI.ViewManagement.UISettings();
        var accentColor = UISettings.GetColorValue(Windows.UI.ViewManagement.UIColorType.Accent);
        accentBrush = new SolidColorBrush(accentColor);
    }
    

    【讨论】:

      猜你喜欢
      • 2020-08-14
      • 2019-07-13
      • 2019-12-21
      • 2017-08-19
      • 1970-01-01
      • 1970-01-01
      • 2018-06-07
      • 2017-07-18
      • 1970-01-01
      相关资源
      最近更新 更多