为什么未加载类库中的图像资源



我有奇怪的问题,我不知道如何找到 - 我在这里寻找similair帖子,但失败了。

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

我在该控件中具有图像背景,上面有标签(用Panel.ZIndex保证(。

在一个项目中,它显示正确,但在另一个项目中仅显示Label,出于某种原因没有显示图像。

有什么问题?我对此感到不知所措...

在一个控件的代码下方:

<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();
  }
}

使用完整的资源文件包URI,包括usercontrol库的汇编名称(不是命名空间(,如下所示。

否则WPF用"本地"组件的名称解析了包装URI,这可能是主要应用程序的名称。

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

还确保将图像文件的构建操作设置为资源。


注意,设置Panel.ZIndex在这里毫无意义。这些元素默认按在XAML中声明的顺序堆叠,因此即使没有设置Zindex,TextBlock始终在图像的顶部。

最新更新