我正在用 UWP (C# & XAML) 撰寫一個應用程式。我有這個代碼:
<Image Source="ms-appx:///Assets/Image.svg"/> <!-- svg is preferred but not required -->
Image.svg 是單色(黑色)影像。我想要實作的是影像會根據系統/應用程式主題(淺色主題 = 黑色影像,深色主題 = 白色影像)改變顏色,而無需分離的白色和黑色影像檔案。
此時,影像為黑色。
甚至可能嗎?
我將不勝感激任何幫助和建議。
uj5u.com熱心網友回復:
應用主題更改時更改影像顏色
對于您的要求,我們建議您使用 BitmapIcon方法。并且BitmapIcon具有前景顏色屬性,您可以在主題更改時動態設定它。另一種方法是為不同的主題定義不同的影像,然后將影像源與ThemeResource標記系結。
例如
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<BitmapIcon
VerticalAlignment="Center"
Foreground="{ThemeResource IconColor}"
UriSource="ms-appx:///Assets/Butterfly.png"
Visibility="Visible" />
<Image
Grid.Row="1"
VerticalAlignment="Center"
Source="{ThemeResource MyImage}" />
</Grid>
資源
<ResourceDictionary.ThemeDictionaries>
<ResourceDictionary x:Key="Light">
<ImageSource x:Key="MyImage">ms-appx:///Assets/png-icon.png</ImageSource>
<SolidColorBrush x:Key="IconColor" Color="Black" />
</ResourceDictionary>
<ResourceDictionary x:Key="Dark">
<ImageSource x:Key="MyImage">ms-appx:///Assets/Butterfly.png</ImageSource>
<SolidColorBrush x:Key="IconColor" Color="White" />
</ResourceDictionary>
</ResourceDictionary.ThemeDictionaries>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/436755.html
