Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
292 views
in Technique[技术] by (71.8m points)

c# - Why is an image resource in a class library not loaded?

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

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

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

Below code of a control:

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

Code behind:

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();
  }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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.


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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...