2015-11-15 13 views
1

我在我的應用程序中遇到問題。在所有屏幕上,我都使用硬編碼的「白色」背景和文本顏色(前景),我使用PhoneAccentBrush。在檢查高對比度選項之前,一切都很好。然後PhoneAccentBrush變成白色,最後在白色背景上顯示白色文字。我檢查我該如何解決這個問題,在我看來,最好的選擇是在高對比度模式下重寫PhoneAccentBrush(我也考慮過轉換器,但是我必須使用很多文本才能在任何地方使用它)。基本上我想要做的是將PhoneAccentBrush黑色設置爲高對比度。我與此戰鬥了幾個小時,仍然沒有任何東西。我檢查了這一點: How to ignore "ease of access" settings on Windows Phone? https://msdn.microsoft.com/library/windows/apps/br208807?f=255&MSPPError=-2147217396和(「頁面底部的高對比度樣本」) 仍然沒有。 當我嘗試使用MSDN例如,它不會在WP(它是完整的Windows preparend)工作 - 當我將此代碼粘貼到的App.xaml如何在高對比度WP8.1上覆蓋PhoneAccentBrush

<Application.Resources> 
    <ResourceDictionary> 
     <ResourceDictionary.MergedDictionaries> 
      <ResourceDictionary Source="Common/StandardStyles.xaml"/> 
      <ResourceDictionary Source="Sample-Utils/SampleTemplateStyles.xaml"/> 
     </ResourceDictionary.MergedDictionaries> 

    </ResourceDictionary> 
</Application.Resources> 

我得到的錯誤: 「每個字典條目必須一個關聯的密鑰「。 「Dictionary Item'ResourceDictionary'必須具有Key屬性」,因此它在Windows Phone上有所不同。從堆棧溢出鏈接的方法我得到相同的,我嘗試給「默認」或「HighContrast」鍵字典,但我不工作。我還找到了MSDN上所有密鑰的列表,但PhoneAccentBrush不存在。有人可以幫助我嗎?

回答

3

您需要創建一個主題資源字典,在你App.xaml文件,在這裏您設置前景刷不同的主題:

<Application.Resources> 
    <ResourceDictionary> 
     <!-- Non-themed resources --> 
     <SolidColorBrush x:Key="PivotHeaderForegroundSelectedBrush" Color="DarkBlue" /> 
     <SolidColorBrush x:Key="PivotHeaderForegroundUnselectedBrush" Color="Gray" /> 

     <!-- Themed resources --> 
     <ResourceDictionary.ThemeDictionaries> 
      <ResourceDictionary x:Key="Dark"> 
       <SolidColorBrush x:Key="MyForegroundBrush" Color="{Binding Source={ThemeResource PhoneAccentBrush}, Path=Color}"/> 
      </ResourceDictionary> 

      <ResourceDictionary x:Key="Light"> 
       <SolidColorBrush x:Key="MyForegroundBrush" Color="{Binding Source={ThemeResource PhoneAccentBrush}, Path=Color}"/> 
      </ResourceDictionary> 

      <ResourceDictionary x:Key="HighContrastBlack"> 
       <SolidColorBrush x:Key="MyForegroundBrush" Color="Black"/> 
      </ResourceDictionary> 

      <ResourceDictionary x:Key="HighContrastWhite"> 
       <SolidColorBrush x:Key="MyForegroundBrush" Color="Black"/> 
      </ResourceDictionary> 

      <ResourceDictionary x:Key="HighContrastCustom"> 
       <SolidColorBrush x:Key="MyForegroundBrush" Color="Black"/> 
      </ResourceDictionary> 
     </ResourceDictionary.ThemeDictionaries> 
    </ResourceDictionary> 
</Application.Resources> 

通知MyForegroundBrush如何使用PhoneAccentBrush在黑暗/光的主題,但在高對比度模式下被硬編碼爲合適的顏色。

在您的XAML中正常使用MyForegroundBrush,並根據當前主題使用正確的版本。

<TextBlock Foreground="{ThemeResource MyForegroundBrush}"> 

只是一個想法,不過,如果用戶設置自己的主題,高對比度黑,他們通常希望有一個黑色的背景。硬編碼在你的應用程序中白色是正確的做法?您可以使用上述方法創建背景畫筆,在Light/High Contrast白色主題中爲白色,而在High Contrast Black主題中爲黑色。

+0

你的explenation是非常好的 - 現在我有更好的方法來解決問題,但仍然當我粘貼上面的代碼到App.xaml我有「字典項目'ResourceDictionary'必須有一個關鍵屬性」。我應該使用什麼密鑰?我嘗試使用「默認」,它編譯,但當我去到與Foreground =「{ThemeResource MyForegroundBrush}」的頁面「我得到錯誤」WinRT信息:無法找到與名稱/密鑰的資源MyForegroundBrush [行:254位置: 40]「 – Piachu

+0

我剛剛創建了一個空白的WP8.1項目,並嘗試了上面的代碼,以防萬一我犯了一個錯誤,但它按預期工作。所以,我不知道爲什麼會出現錯誤 - 每個ResourceDictionary都有一個密鑰,而且密鑰是有效的。您的app.xaml中是否存在導致問題的其他內容? –

+0

確實在空白項目中工作正常; /這裏是App.xaml的所有代碼,給我這個關鍵錯誤http://pastebin.com/0eA5RTGU,當我嘗試像上面這樣移動ThemeDictionary:http://pastebin.com/ GzEeFYCT我有錯誤:「此會員'資源'有多個項目,使用項目屬性」所以我認爲我們正在接近,但可悲的是我沒有看到任何或<應用。Resources.Items> – Piachu

0

我也在資源字典中遇到過丟失的PhoneAccentBrush。原來,我刪除了默認的頁面背景,指向資源字典,當我把它放回它的工作:背景=「{ThemeResource ApplicationPageBackgroundThemeBrush}」

相關問題