如下回答來自:Images in a WPF Custom Control Library和How 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作爲一種資源:
將生成操作設置爲資源。
我曾嘗試將文件添加到持有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>
在設計視圖中,我可以看到圖像的預覽,它告訴我的路徑是正確的。 但是,輸出仍然不會產生圖像。
我猜這是某種構建順序問題? 所有幫助表示讚賞。
編輯
切換圖像的嵌入的資源的窗口,但不是控制工作。
EDIT2
切換到嵌入式資源工程控制庫然而,圖像也需要在運行控制的解決方案。有沒有一種方法可以讓圖像來自DLL並且不需要用戶引用它?
嘗試將您的'imageone.png'文件的_Build Action_設置爲_Content_或_Embedded Resource_並重新運行您的應用程序。 –
@Ash,OP表示:_將構建動作設置爲resource_ –
切換到_embeded resource_而不是_resource_可用於窗口中顯示的圖像,但不能控制 –