1

I am trying to display an image using the latest version of winui, without success. Due to my lack of progress I assume I must be doing something terribly wrong, however, I can't determine what that might be.

Here is my most recent attempt.

MainWindow.xaml:

<Image Source="AppIcon.png" Width="200" Height="200" ImageFailed="ImageFailed_Event"/>

Image

3
  • Why did you add the WPF tag to your question? Do you know how to set the Image's Source property in WPF and does that not work in WinUI? Is the ImageFailed event fired? What's in the EventArgs then? Commented Apr 5, 2021 at 11:43
  • @Clemens, I added the WPF tag because WPF and winui appear to be very similar. The tags appear to be ordered incorrectly, however, I was unable to reorder them, as such I removed it. Yes, the ImageFailed event is fired, the ErrorMessage is "E_NETWORK_ERROR" oddly enough. Commented Apr 5, 2021 at 14:24
  • 2
    Can you try using Source="/AppIcon.png"? Also verify whether the AppIcon.png build action is set to Content and CopyIfNewer Commented Apr 6, 2021 at 15:46

1 Answer 1

3

Ok here is the solution that I have found with the help of the people in the comments:

  • Set the "Build Action" of the image file to "Content"
  • Set the "Copy to Output Directory" to "Copy if newer"

This is all that is necessary, no URI magic is needed.

Sign up to request clarification or add additional context in comments.

5 Comments

Not sure why the down vote. Your issue was that the image file you were trying to use was not deployed together with the application. The above solution fixes it. I too was wondering if some specific URI was needed.
This does indeed fix it and is the solution. I have upvoted.
Did not work with WinUI3
@JohnGlen you may need to use "ms-appx:///directory/file.png" as the URI.
It does matter where you place the image within your solution. For example, if it had been in the wrong project, your instructions above would not be enough to resolve...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.