Cannot load <Image> in App.xaml - ControlTemplate
See original GitHub issueI 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:
- Created 4 years ago
- Comments:21 (9 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.:Note the
MyAssembly
part. Previously you were trying withSource="avares://Assets/logo_vertical_100w.png"
and so it was looking for an assembly calledAssets
.For more info see the docs: http://avaloniaui.net/docs/quickstart/assets
Is the build action set to AvaloniaResource?