【发布时间】:2017-03-22 17:05:14
【问题描述】:
以下来自 Images in a WPF Custom Control Library 和 How can I get a BitmapImage from a Resource? 的回答
我做了一个简单的自定义控件库:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfCustomControlLibrary1">
<Style TargetType="{x:Type local:CustomControl1}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:CustomControl1}">
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<Grid>
<StackPanel Orientation="Vertical">
<StackPanel Orientation="Horizontal">
<Label Content="imageone.png" />
<Image Source="imageone.png" />
</StackPanel>
<StackPanel Orientation="Horizontal">
<Label Content="/imageone.png" />
<Image Source="/imageone.png" />
</StackPanel>
<StackPanel Orientation="Horizontal">
<Label Content="Resources/imageone.png" />
<Image Source="Resources/imageone.png" />
</StackPanel>
<StackPanel Orientation="Horizontal">
<Label Content="/Resources/imageone.png" />
<Image Source="/Resources/imageone.png" />
</StackPanel>
<StackPanel Orientation="Horizontal">
<Label Content="../Resources/imageone.png" />
<Image Source="../Resources/imageone.png" />
</StackPanel>
<StackPanel Orientation="Horizontal">
<Label Content="..Resources/imageone.png" />
<Image Source="..Resources/imageone.png" />
</StackPanel>
<StackPanel Orientation="Horizontal">
<Label Content="..//Resources//imageone.png" />
<Image Source="..//Resources//imageone.png" />
</StackPanel>
<StackPanel Orientation="Horizontal">
<Label Content="pack://application:,,,/imageone.png" />
<Image Source="pack://application:,,,/imageone.png" />
</StackPanel>
<StackPanel Orientation="Horizontal">
<Label Content="pack://application:,,,/Resources/imageone.png"/>
<Image Source="pack://application:,,,/Resources/imageone.png"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<Label Content="pack://application:,,,/WpfCustomControlLibrary1;v1.0.0.0;Resources/imageone.png"/>
<Image Source="pack://application:,,,/WpfCustomControlLibrary1;v1.0.0.0;Resources/imageone.png"/>
</StackPanel>
</StackPanel>
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
并添加 imageone.png 作为资源:
将构建操作设置为资源。
我已尝试将该文件添加到包含 generic.xaml 文件的主题文件夹中。并相应地改变了路径,但它仍然产生这个输出:
没有图片。
在自定义控件库中引用图像的正确方法是什么?
我也尝试在应用程序中引用图像:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication1"
xmlns:custom="clr-namespace:WpfCustomControlLibrary1;assembly=WpfCustomControlLibrary1"
mc:Ignorable="d"
Title="MainWindow" Height="500" Width="500">
<Grid>
<!--<custom:CustomControl1></custom:CustomControl1>-->
<StackPanel Orientation="Vertical">
<StackPanel Orientation="Horizontal">
<Label Content="Resources/imageone.png" />
<Image Width="20" Height="20" Source="Resources/imageone.png" />
</StackPanel>
<StackPanel Orientation="Horizontal">
<Label Content="/Resources/imageone.png" />
<Image Width="20" Height="20" Source="/Resources/imageone.png" />
</StackPanel>
<StackPanel Orientation="Horizontal">
<Label Content="pack://application:,,,/Resources/imageone.png"/>
<Image Width="20" Height="20" Source="pack://application:,,,/Resources/imageone.png"/>
</StackPanel>
</StackPanel>
</Grid>
在设计器视图中,我可以看到图像的预览,告诉我路径是正确的。 但是输出仍然不会产生图像。
我猜这是某种构建顺序问题? 感谢所有帮助。
编辑
将图像切换到嵌入式资源适用于窗口,但不适用于控件。
编辑2
切换到嵌入式资源适用于控件库,但是,图像还需要位于运行控件的解决方案中。有没有办法让图像只来自 DLL 而不需要用户引用它?
【问题讨论】:
-
尝试将
imageone.png文件的 Build Action 设置为 Content 或 Embedded Resource 并重新运行您的应用程序. -
@Ash,OP 说:将构建操作设置为资源
-
切换到 embeded resource 而不是 resource 对窗口中显示的图像有效,但对控件无效
-
如果您尝试引用的图像在另一个程序集中,那么您必须在包 URI 中包含该程序集的名称。
-
那么 Image 的 Source 应该是
pack://application:,,,/Themes/imageone.png(或 XAML 中的缩写Themes/imageone.png)。包含程序集名称的完整包 URI 将是pack://application:,,,/WpfCustomControlLibrary1;component/Themes/imageone.png(如果WpfCustomControlLibrary1实际上是程序集名称)