为什么未加载类库中的图像资源? [英] Why is an image resource in a class library not loaded?

查看:25
本文介绍了为什么未加载类库中的图像资源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个奇怪的问题,我不知道该怎么找到-我在这里寻找了类似的帖子,但是失败了.

I have strange problem, which I don't know how to find - I looked for similair posts here, but failed.

问题是我在WPF中具有自定义控件,显然,我想在多个项目中重用它.

Problem is that I have custom control in WPF and, obviously, I want to reuse it in multiple projects.

该控件中有图像背景,并带有标签(由 Panel.ZIndex 保证).

I have image background in that control with label over it (assured with Panel.ZIndex).

在一个项目中它可以正确显示,而在另一个项目中,

In one project it is showing correctly, but in another just Label is showing, image for some reason does no display.

可能是什么问题?我对此感到迷茫...

What could problem be? I am loosing my mind over this...

下面的控件代码:

<UserControl x:Class="SampleControls.LabelWithBoxBackground"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:SampleControls"
             mc:Ignorable="d" 
             d:DesignHeight="150" d:DesignWidth="400" x:Name="labelWithBoxBackground">
    <Grid>
        <Image Source="pack://application:,,,/Images/boxImage.png" Stretch="Fill" Panel.ZIndex="1"/>
        <TextBlock Background="White" Text="{Binding ElementName=labelWithBoxBackground, Path=Text}" Margin="0,20,0,0" Panel.ZIndex="2" HorizontalAlignment="Center" VerticalAlignment="Center" FontWeight="Bold" FontFamily="Calibri"/>
    </Grid>
</UserControl>

后面的代码:

public partial class LabelWithBoxBackground : UserControl
{
  public static readonly DependencyProperty TextProperty =
    DependencyProperty.Register("Text", typeof(string), typeof(LabelWithBoxBackground), new FrameworkPropertyMetadata(string.Empty));

  public string Text
  {
    get { return GetValue(TextProperty).ToString(); }
    set { SetValue(TextProperty, value); }
  }

  public LabelWithBoxBackground()
  {
    InitializeComponent();
  }
}

推荐答案

使用完整的

Use a full Resource File Pack URI, including the assembly name (not the namespace) of your UserControl library, as shown below.

否则,WPF会使用本地"程序集的名称来解析Pack URI,该名称可能是主应用程序的名称.

Otherwise WPF resolves the Pack URI with the name of the "local" assembly, which may be that of the main application.

Source="pack://application:,,,/SampleControls;component/Images/boxImage.png"

还要确保将图像文件的生成操作"设置为资源".

Also make sure that the Build Action of the image file is set to Resource.

请注意,此处设置 Panel.ZIndex 毫无意义.默认情况下,元素按照在XAML中声明的顺序堆叠,因此,即使不设置ZIndex,TextBlock也始终位于Image的顶部.

As a note, setting Panel.ZIndex is pointless here. The elements are stacked by default in the order they are declared in XAML, so the TextBlock is always on top of the Image, even without setting ZIndex.

这篇关于为什么未加载类库中的图像资源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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