WPF自定义控件库图像资源

时间:2017-03-22 17:05:14

标签: c# wpf xaml

以下答案来自:Images in a WPF Custom Control LibraryHow 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作为资源:

enter image description here

将构建操作设置为资源。

我尝试将文件添加到包含generic.xaml文件的themes文件夹中。并相应地更改了路径,但它仍然产生了这个输出:

enter image description here

没有图片。

在自定义控件库中引用图像的正确方法是什么?

我还尝试在应用程序中引用图像:

<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>

在设计师视图中我可以看到图像的预览,告诉我路径是正确的。 但是输出仍然不会产生图像。

我猜它是某种构建顺序问题? 所有帮助表示赞赏。

修改

将图像切换为嵌入式资源适用于窗口但不适用于控件。

EDIT2

切换到嵌入式资源适用于控件库,但是,映像也需要位于运行控件的解决方案中。有没有一种方法,图像只是来自DLL而不需要用户引用它?

1 个答案:

答案 0 :(得分:2)

尝试将imageone.png文件的构建操作设置为内容嵌入式资源,然后重新运行您的应用程序。