2017-04-24 43 views
2

我試圖獲取當前用戶界面,強調色彩使用下面的代碼UWP平臺上:UWP v10240 GetAccentColor

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 [大會Windows.Foundation.UniversalApiContract,版本定義= 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

回答

2

這看起來像在合同中上市的錯誤,然後流入文檔:

一個展示項目,在給定的。

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