2017-03-22 81 views
2

如下回答来自:Images in a WPF Custom Control LibraryHow can I get a BitmapImage from a Resource? WPF自定义控件库图像资源

我做了一个简单的自定义控件库:

<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文件的主题文件夹中。没有图像

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

尝试将您的'imageone.png'文件的_Build Action_设置为_Content_或_Embedded Resource_并重新运行您的应用程序。 –

+3

@Ash,OP表示:_将构建动作设置为resource_ –

+0

切换到_embeded resource_而不是_resource_可用于窗口中显示的图像,但不能控制 –

回答

2

尝试为您imageone.png文件中设置生成操作要么内容嵌入的资源并重新运行你的应用程序。

+0

用原文中的答案解释问题 –

+0

回到原点 –