【发布时间】:2012-03-06 15:48:21
【问题描述】:
我尝试将图像加载到我的 WPF 项目的主窗口中。首先,我创建了名为 Images.resx 的 RESX 文件(它位于 Properties 文件夹中)。然后我在这个资源中添加了一个名为 logo.png 的图像(使用 VS2010 RESX 编辑器)。图像已正确复制到资源文件夹中。然后我将 Build Action 属性从 Embedded Resource 更改为 Resource。在此之后,我想在 XAML 中加载我的图像。
<Image x:Name="image" Source="/MyProject;component/Resources/Logo.png" />
在 Expression Blend 的预览中出现了一张图片,但是当我编译程序时,有一个空窗口(没有图片)。
接下来我使用了 ResourceDictionary。我创建了新文件 - Theme.xaml 并添加了这个:
<BitmapImage x:Key="logo" UriSource="/MyProject;component/Resources/Logo.png" />
在 MainWindow.xaml 中:
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Styles/Theme.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<Image x:Name="image" Source="{StaticResource logo}" />
在 Expression Blend 中再次正常,但编译后...什么也没有出现:/
我尝试将 Build Action 改回 Embedded Resource。什么都没发生。
谁能告诉我如何在 XAML 中使用来自 RESX 文件的嵌入式资源? 提前致谢。
【问题讨论】:
-
为什么首先将它们添加到 resx 中?你对它们进行本地化吗?如果是这样,为什么还要使用绝对路径?
标签: wpf image xaml resources resx