[英]I want to change image source on code behind, like theme
I have images on my xaml. 我的xaml上有图像。 like these...
像这些...
<Image Name="ImageTekSayfa" Source="Resources/Dark/teksayfa.png" Margin="2,0" MouseLeftButtonUp="TekSayfa_Clicked"/>
<Image Name="ImageFavorites" Source="Resources/Dark/favorites.png" Margin="2,0" MouseLeftButtonUp="FavoritesClick"/>
<Image Name="ImageSoruModu" Source="Resources/Dark/sorumodu.png" Margin="2,0" MouseLeftButtonUp="SoruModu_Clicked"/>
<Image Name="ImagePerde" Source="Resources/Dark/tahta.png" Margin="2,0" MouseLeftButtonUp="PerdeClicked"/>
<Image Name="ImagePen" Source="Resources/Dark/kalem.png" Margin="2,0" MouseLeftButtonUp="DrawingClicked"/>
<Image Name="ImageEraser" Source="Resources/Dark/silgi.png" Margin="2,0" MouseLeftButtonUp="EraserClicked"/>
And I want to write my ImageSources like this and change all Source path easy on my code side. 我想这样编写ImageSources并在代码侧轻松更改所有Source路径。 like this:
像这样:
<Image Name="ImageTekSayfa" Source="Resources/{THEME_NAME}/teksayfa.png" Margin="2,0" MouseLeftButtonUp="TekSayfa_Clicked"/>
public String THEME_NAME="dark";
if(currentTheme="ligth")
THEME_NAME="light";
etc.. 等等..
Two possible methods: 两种可能的方法:
ValueConverter - Store each "theme templated" source as a string in a resource dictionary and bind the Image Source using a custom implementation of IValueConverter that correctly populates the required Uri: ValueConverter-将每个“主题模板化”源作为字符串存储在资源字典中,并使用IValueConverter的自定义实现绑定图像源,该实现正确填充了所需的Uri:
<Image Name="ImageTekSayfa" Source="{Binding Source={StaticResource teksayfa}, Converter={StaticResource themeConverter}}" Margin="2,0" MouseLeftButtonUp="TekSayfa_Clicked"/>
Custom XAML Behavior - Create a custom behavior that can convert the "theme templated" source into the required Uri: 自定义XAML行为-创建可将“主题模板化”源转换为所需Uri的自定义行为:
<Image Name="ImageTekSayfa" Margin="2,0" MouseLeftButtonUp="TekSayfa_Clicked"> <i:Interaction.Behaviors> <bhv:ThemeImageSourceBehavior Source="Resources/{THEME_NAME}/teksayfa.png" /> </i:Interaction.Behaviors> </Image>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.