2

I currently have a very simple UserControl that only contains a single image. When I put this UserControl onto another window in the same project, the image no longer appears in the editor, nor does it appear when I run my program. I've looked at every question I can find on this, but I still haven't been able to get this working.

My user control is:

<UserControl x:Class="MyApp.DetailViews.InfoDetails"
         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" 
         mc:Ignorable="d" 
         d:DesignHeight="20" d:DesignWidth="20">
<Grid>
    <Image Source="/MyApp;component/Assets/Icon.png" HorizontalAlignment="Left" Height="20" VerticalAlignment="Top" Width="20"/>
</Grid>

I've checked these quesstions:

Embedded images not showing when in a UserControl

WPF - UserControl with images not showing properly

Why are my images not showing up in my UserControl?

Image in WPF Button not Visible at Runtime

The thing they seem to all have in common is the build settings for the components. As such, I've set the actual .png build action to Content and the Copy to Output Directory to Copy always.

The actual UserControl itself has it's Build Action set to page (although I've tried setting it to Content or Resource), and is set to Do not copy. These are the same settings as all my other UserControls, and I have no issues with those.

I have the following folder structure:

MyApp/MyApp.sln
MyApp/MainWindow.xaml/cs
MyApp/DetailViews/InfoDetails.xaml/cs
MyApp/Assets/Icon.png

What have I missed to make the image actually appear on the window?

Thanks.

Community
  • 1
  • 1
D Marks
  • 21
  • 1
  • 4
  • 1
    if you use this image on an image control(not inside user control) will it load? – Dark Templar Jul 07 '16 at 23:40
  • What is the relationship of the `UserControl` to the window in which you're using it? Are they in the same project? Are they in the same folder in the same project? It's not likely anyone will be able to fix your problem without a _complete_ description of how your project(s) is(are) structured, including where and how the resource is being declared. – Peter Duniho Jul 08 '16 at 01:20
  • As `/MyApp;component/Assets/Icon.png` is a [Resource File Pack URI](https://msdn.microsoft.com/en-us/library/aa970069(v=vs.100).aspx#Resource_File_Pack_URIs___Local_Assembly), the Build Action of the image file must be `Resource`. Then you would also select `Do not copy`. – Clemens Jul 08 '16 at 05:38
  • @DarkTemplar, the image loads fine in it's own control on the main window – D Marks Jul 10 '16 at 12:46
  • @PeterDuniho I've edited the question with those details. This is all in a single project – D Marks Jul 10 '16 at 12:55
  • Additionally, I would have thought I could put this UserControl on any other Window or UserControl regardless of project layout (In fact, I plan to later). Is this not the case? – D Marks Jul 10 '16 at 13:06

1 Answers1

4

Try using full pack URI.

Source="pack://application:,,,/MyApp;component/Assets/Icon.png"

For build action you can leave it at "None", but you must select "Copy if newer".

Jai
  • 8,165
  • 2
  • 21
  • 52
  • 1
    This is a [Resource File Pack URI](https://msdn.microsoft.com/en-us/library/aa970069(v=vs.100).aspx#Resource_File_Pack_URIs___Local_Assembly), so the Build Action must of course be `Resource`. Then you would also select `Do not copy`. Besides that, the `pack://application:,,,` prefix isn't necessary in XAML. It is added automatically. – Clemens Jul 08 '16 at 05:32
  • @Clemens Content File Pack URI also uses the exact same syntax. And also, `pack://application:,,,` prefix denotes absolute path; excluding it means relative path. It's all mentioned in the same URL you provided. – Jai Jul 08 '16 at 06:34
  • With the difference that setting the `Content` Build Action just doesn't work with loading image files from Pack URIs. Give it a try. – Clemens Jul 08 '16 at 06:58
  • 1
    @Clemens I tried with `Content` and `Copy if newer`, it worked. – Jai Jul 08 '16 at 08:44
  • Ok, I see. My understanding is still that a content file should be loaded by a relative file URL, whereas a Resource file is loaded by a Pack URI. – Clemens Jul 08 '16 at 09:44
  • For me, using full pack URI doesn't seem to make a difference. Switching settings between None/Content/Resource doesn't help either. – D Marks Jul 10 '16 at 13:17