资源项目中的图像未加载到嵌套控件中 [英] Images in resource project are not loading inside nested controls

查看:72
本文介绍了资源项目中的图像未加载到嵌套控件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为简洁起见,我删除了一些代码,但是我的问题是,在运行时所包含的控件中没有从资源项目中加载图像.xaml与图像位于不同的项目中,但是两者都在相同的解决方案中,并且xaml项目以资源项目为参考.

I've removed some code for brevity, but my problem is that images are not loading from resource project at runtime in included controls. The xaml is in a different project than the images, but both are in the same solution and the xaml project has the resource project as a reference.

图像从 Source ="/ThinkVisual.Resources; component/Images/save.png"/加载,但位于< my:OwnerDetails x:Name ="controlOwnerDetails"/> 中有一张无法加载的图片.

The image loads from Source="/ThinkVisual.Resources;component/Images/save.png"/ but inside <my:OwnerDetails x:Name="controlOwnerDetails" /> there is an image which does not load.

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

父控件和子控件都在同一项目和文件夹中.

Both the parent and the child controls are in the same project and folder.

<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中可见,但在执行过程中不会显示,项目构建和运行时不会出现错误.

The images are visible in Visual Studio but not displayed during execution, project builds and runs without error.

推荐答案

您使用了错误的确保将自定义控件库中的图像设置为 Resource Build Action,并将 False 设置为复制到输出目录".

Make sure your Image in your custom control library is set to Resource Build Action and False for Copy to Output Directory.

然后将您的 Image.Source 更改为:

<Image Width="16" 
       Height="16" 
       Source="pack//application:,,,/ThinkVisual.Resources;component/Images/add_button.png" />

就我个人而言,我会将XAML中使用的所有图像与XAML放在同一个项目中,以打破依赖关系.如果有人拿了您的程序集并想重复使用它,请显示该图像.他们将需要使用相同的命名约定和文件名创建一个新项目,以便有意使用该程序集.

Personally, I would put all the images used in XAML in the same project as the XAML to break the dependency. Image if someone took your assembly and wanted to reuse it. They would need to create a new project with the same naming convention and file names in order to use the assembly with it's intention.

如果在两个程序集(项目)中都使用了该图像,则将图像添加到每个程序集中,并使用适当的Pack Uri.

If the image is used in both assemblies (projects), add the images to each assembly and use proper Pack Uri's.

因此,如果您在 ThinkVisual.Administration 中定义的控件中使用图像,请使用

So if you're using the image in a control defined in ThinkVisual.Administration then use

Source="pack//application:,,,/ThinkVisual.Adminstration;component/Images/add_button.png"

,如果您在 ThinkVisual.Resources 中使用的控件使用相同的图像,则对 Source 使用第一个代码片段.

and if the control you're using in ThinkVisual.Resources uses the same image, then use the first snippet for Source.

这篇关于资源项目中的图像未加载到嵌套控件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆