为了简洁,我删除了一些代码,但我的问题是图像不是在运行时在包含的控件中从资源项目加载的。 xaml与图像位于不同的项目中,但两者都在同一个解决方案中,xaml项目将资源项目作为参考。
图片从Source="/ThinkVisual.Resources;component/Images/save.png"/
加载但在<my:OwnerDetails x:Name="controlOwnerDetails" />
内部有一张图片无法加载。
<Button x:Name="btnAddOwnerDetails" ToolTip="Add Owner/Management details" Click="btnAddOwnerDetails_Click">
<Image Width="16" Height="16" Source="/ThinkVisual.Resources;component/Images/add_button.png" />
</Button>
父控件和子控件都在同一个项目和文件夹中。
<lc:LayoutGroup x:Name="layoutCompanyDetails" Header="Company Details" View="GroupBox" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Orientation="Vertical" <lc:LayoutGroup x:Name="tabCompanyDetails" View="Tabs" SelectedTabChildChanged="tabCompanyDetails_SelectedTabChildChanged">
<lc:LayoutGroup Header="Details">
<my:CompanyDetails x:Name="controlCompanyDetails" />
</lc:LayoutGroup>
<lc:LayoutGroup Header="Owner/Management Details">
<my:OwnerDetails x:Name="controlOwnerDetails" />
</lc:LayoutGroup>
</lc:LayoutGroup>
<lc:LayoutItem>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
<Button x:Name="btnUpdate" ToolTip="Update company details" Padding="0" Margin="0" Width="65" MouseEnter="Button_MouseEnter" MouseLeave="Button_MouseLeave" IsEnabled="{Binding Path=AllPropertiesValid}" Click="btnUpdate_Click">
<Button.Content>
<StackPanel Orientation="Horizontal">
<Image Width="16" Height="16" Source="/ThinkVisual.Resources;component/Images/save.png"/>
<Label x:Name="lblSaveButton" Padding="2">Update</Label>
</StackPanel>
</Button.Content>
</Button>
</StackPanel>
</lc:LayoutItem>
图像在Visual Studio中可见,但在执行,项目构建和运行期间不会显示错误。
答案 0 :(得分:0)
您使用了错误的Pack URI
确保自定义控件库中的图像设置为Resource
构建操作,False
复制到输出目录。
然后将Image.Source
更改为:
<Image Width="16"
Height="16"
Source="pack//application:,,,/ThinkVisual.Resources;component/Images/add_button.png" />
就个人而言,我会将XAML中使用的所有图像放在与XAML相同的项目中,以打破依赖性。图像,如果有人拿你的组件,并想重新使用它。他们需要创建一个具有相同命名约定和文件名的新项目,以便根据其意图使用程序集。
如果图像在两个程序集(项目)中使用,请将图像添加到每个程序集并使用正确的Pack Uri。
因此,如果您在ThinkVisual.Administration
中定义的控件中使用该图像,请使用
Source="pack//application:,,,/ThinkVisual.Adminstration;component/Images/add_button.png"
如果您在ThinkVisual.Resources
中使用的控件使用相同的图片,请使用Source
的第一个代码段。