question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot load <Image> in App.xaml - ControlTemplate

See original GitHub issue

I have the following items:

  • /Assets folder containing some files like /Assets/logo_vertical_100w.png and /Assets/icon_gear.png. These files have BuildAction=None
  • In the root, /App.xaml file where I declare a <ControlTemplate> like this (simplified):
<Application xmlns="https://github.com/avaloniaui"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:local="clr-namespace:MyProject.Desktop"
             xmlns:base="clr-namespace:MyProject.Desktop"
             xmlns:br="clr-namespace:MyProject.Desktop.Views"
             x:Class="MyProject.Desktop.App">
[...]
<ControlTemplate x:Key="MainWindowTemplate">
    <StackPanel>
        <Image Source="/Assets/logo_vertical_100w.png" />
    </StackPanel>
</ControlTemplate>

When running I receive an error for the logo image in App.xaml:

  • If I declare it with Source="/Assets/logo_vertical_100w.png":

Portable.Xaml.XamlObjectWriterException: 'Could not convert object '/Assets/logo_vertical_100w.png' (of type System.String) to {clr-namespace:Avalonia.Media.Imaging;assembly=Avalonia.Visuals}IBitmap: Relative uri /Assets/logo_vertical_100w.png without base url'

  • Tried also with Source="avares://Assets/logo_vertical_100w.png", but:

Portable.Xaml.XamlObjectWriterException: 'Could not convert object '/Assets/logo_vertical_100w.png' (of type System.String) to {clr-namespace:Avalonia.Media.Imaging;assembly=Avalonia.Visuals}IBitmap: Could not load file or assembly 'Assets, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified.

What’s the correct way to set Image’s source in shared templates?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:21 (9 by maintainers)

github_iconTop GitHub Comments

2reactions
grokyscommented, May 13, 2019

Just investigated this and it seems to be a Portable.Xaml problem in that it’s not looking in the right place for the base URL. I will look into fixing it, but given we’re moving to a XAML compiler hopefully soon, I’m not sure how much time is worth spending on it.

In the meantime, the Source="avares://" syntax should work. However you need to actually supply the name of your assembly in the URL e.g.:

Source="avares://MyAssembly/Assets/logo_vertical_100w.png"

Note the MyAssembly part. Previously you were trying with Source="avares://Assets/logo_vertical_100w.png" and so it was looking for an assembly called Assets.

For more info see the docs: http://avaloniaui.net/docs/quickstart/assets

1reaction
Gillibaldcommented, May 18, 2019

Is the build action set to AvaloniaResource?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Image Source In ControlTemplate WPF
1) In XAML using binding where the source of the binding will be some object's property containing the image source (this is the...
Read more >
Xamarin.Forms control templates
Xamarin.Forms control templates define the visual structure of ContentView derived custom controls, and ContentPage derived pages.
Read more >
The Image control - The complete WPF tutorial
It controls what happens when the dimensions of the image loaded doesn't completely match the dimensions of the Image control. This will happen...
Read more >
WPF Image control not showing resource image
I am able to set background image for the grid, I just wasn't able to add an Image (control) to the grid and...
Read more >
WPF Templates | Control Template | Part 1 - YouTube
Your browser can't play this video. Learn more. Heightened focus.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found