我来自一个主要是Web和一点点Windows窗体背景.对于一个新项目,我们将使用WPF.WPF应用程序需要10-20个小图标和图像用于说明目的.我正在考虑将它们作为嵌入式资源存储在程序集中.这是正确的方法吗?
如何在XAML中指定Image控件应从嵌入式资源加载图像?
全部,我有一个小应用程序,它检查.resx文件以确保嵌入式括号的一致性(以便不会发生不匹配的"... {0}"字符串的运行时错误).我有MainWindow.xaml的以下XAML,我的特殊问题涉及按钮上显示的图像
<Window x:Class="ResxChecker.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="174.383" Width="495.869">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="350*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="30*"/>
</Grid.RowDefinitions>
<Label Content="Select .resx file:" HorizontalAlignment="Left" Margin="10,0,0,0" VerticalAlignment="Top" Height="24" Width="Auto" Grid.ColumnSpan="1"/>
<TextBox Grid.ColumnSpan="2" HorizontalAlignment="Stretch" Margin="10,0,0,0" Grid.Row="1" TextWrapping="Wrap" Text="" VerticalAlignment="Top"/>
<Button Grid.Column="2" HorizontalAlignment="Right" Margin="5,0,10,0" Grid.Row="1">
<Image VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Height="16 " Width="16" Source="pack://siteoforigin:,,,/Resources/UserCost2013Open16.png"/>
</Button>
</Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)
图像具有"构建操作=资源","复制到输出目录=不复制" - 图像显示在设计器中但不在运行时显示.我已经看到了以下问题并阅读了相关答案,但没有解决问题:
如何让按钮图像在运行时出现?
我的程序集包含一个BuildAction == Resource的图像.我想从这个嵌入式资源中获取BitmapImage.
我可以从这样的文件加载BitmapImage:
var bitmap = new BitmapImage(new Uri(path));
Run Code Online (Sandbox Code Playgroud)
但是如何创建一个将引用嵌入式资源图像的Uri呢?
当我尝试创建' pack URI '(例如pack://application:,,,/MyImage.png或pack://application:,,,/MyAssembly;component/MyImage.png)时,会抛出异常:
System.UriFormatException"无效的URI:由于存在冒号(':')而无法解析端口,因此需要端口."
我在这篇博客文章中找到了修复UriFormatException
但是,应用该修复程序后,我仍然会尝试从包URI加载BitmapImage.
使用pack://application:,,,/Image.png格式时,我得到一个NullReferenceException,当使用该pack://application:,,,/AssemblyName;component/Image.png格式时,我得到一个NotSupportedException"无法识别Uri前缀".
总结 我的问题是我在实例化任何WPF控件/窗口/等之前尝试在进程中使用'pack URI',因此'pack'URI方案尚未注册(其他WPF必需'stuff'也必须不要设置,因为手动注册包方案本身不能解决问题).解决方案是等到实例化我的WPF用户控件后才使用包URI.
我有几个图像,我想嵌入到exe中.
当我将构建操作设置为嵌入式资源时, 我解决了代码中出现资源不可用的错误,并要求我将构建操作设置为资源
我尝试了几种不同的方法:
<ImageSource x:Key="Image_Background">YearBook;component/Resources/Images/darkaurora.png</ImageSource>
<ImageSource x:Key="Image_Background">Images/darkaurora.png</ImageSource>
<ImageSource x:Key="Image_Background">pack://application:,,,/Resources/Images/darkaurora.png</ImageSource>
Run Code Online (Sandbox Code Playgroud)
此代码位于Resource文件中.但没有一个工作,他们都抛出这个错误:
Cannot convert the string 'pack://application:,,,/Resources/Images/darkaurora.png' into a 'System.Windows.Media.ImageSource' object. Cannot locate resource 'resources/images/darkaurora.png'. Error at object 'Image_Background' in markup file 'YearBook;component/Resources/ImageResources.xaml' Line 4 Position 6.
Run Code Online (Sandbox Code Playgroud)
在代码中的不同位置我得到:
the file 'YearBook;component/Resources/Images/shadowdrop.png' is not a part of the project or its 'Build Action' property is not set to 'Resource'
Run Code Online (Sandbox Code Playgroud)
那么,我做错了什么?
我需要动态更改应用于我的一个按钮的背景图像,但无法弄清楚如何.图像将添加到项目中,并将其"构建操作"设置为"资源".我试过以下:
buttonUnits.Background = new ImageBrush(new BitmapImage(new Uri("/Images/InchDOWN.png",UriKind.Relative)));
Run Code Online (Sandbox Code Playgroud)
这成功编译,但会在DirectoryNotFoundException崩溃时说"无法找到路径的一部分'C:\ Images\InchDOWN.png'."
我不希望应用程序在磁盘上查找图像.如何将图像用作嵌入式资源?我认为它涉及将构建操作更改为嵌入式资源,但如何在后面的代码中使用此资源?