XAML图像资源

时间:2014-02-26 14:44:35

标签: c# wpf xaml

我将一些图片作为资源添加到我的项目中(例如我将它们放入文件夹" ressources")。 现在我将它们用于:

<Image Name="_ImageName" Width="100" Height="100" Source="Resources\HalloPicA.png"/>
<Image Name="_ImageName" Width="100" Height="100" Source="Resources\HalloPicB.png"/>

......等等。

想象一下,现在我将更改文件夹&#34; Resources&#34;到&#34; MyResources&#34;。问题我必须在XAML代码中随处更改它。 存在更好的方式吗?在c#-Code中,我将从typ字符串声明一个成员变量,如:

privat _FolderName = "Resources";

并以这种方式使用此成员变量:

Image newImage = Image.FromFile(_FolderName +"HalloPicA.png");

希望我的问题很清楚。

2 个答案:

答案 0 :(得分:2)

您可以使用Property Binding和Value Converters来执行此操作。 例如:

    <Image Name="_ImageName" Width="100" Height="100" Source="{Binding Path=imgA, Converter={StaticResource imgPathConverter}}" />

您的imgPathConverter是可以在“合格”图片路径中转换图片名称的类。

答案 1 :(得分:1)

您可以做的是创建单独的资源字典,或将其放在Application.Resources中,并使用BitmapImage资源:

<Application ...>
    <Application.Resources>
        <BitmapImage UriSource="/MyAssemblyName;component/MyPath/myImage.png" x:Key="imageKey"/>
    </Application.Resources>
</Application>

然后当你想使用它时

<Image Width="100" Height="100" Source="{StaticResource imageKey}"/>

这样,当文件夹更改时,您只需要在一个文件中更改它

相关问题