I have a logo, which is used in certain places of my application. So I'd like to store it's path in a variable of my App.axaml
file, which should allow me to reference this path as variable in the entire application. This works fine with colors like StepBoxBG
<Application.Resources>
<Color x:Key="StepBoxBG">#5eba00</Color>
<Image x:Key="LogoPath">/Assets/Logos/logo.png</Image>
</Application.Resources>
which I reference using DynamicResource
in e.g. a border element like this
<Border Background="{DynamicResource StepBoxBG}" Padding="20">
...
</Border>
But when my logo path is referenced in the same way
<Image Height="90" Source="{DynamicResource LogoPath}" />
no logo is displayed. The path is correct, because when I use the path directly in the Image
element it works:
<Image Height="90" Source="/Assets/Logos/logo.png" />
I found this question and tried it, so the App.axaml
looks like this:
<Application xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:My.App"
xmlns:imaging="clr-namespace:Avalonia.Media.Imaging;assembly=Avalonia.Visuals"
x:Class="ULabs.Image2Card.App">
<Application.DataTemplates>
<local:ViewLocator/>
</Application.DataTemplates>
<Application.Styles>
<FluentTheme Mode="Light"/>
</Application.Styles>
<Application.Resources>
<Color x:Key="StepBoxBG">#5eba00</Color>
<imaging:Bitmap x:Key="LogoPath">
<x:Arguments>
<x:String>/Assets/Logos/logo.png</x:String>
</x:Arguments>
</imaging:Bitmap>
</Application.Resources>
</Application>
Now it throws an exception, because it refers this as an absolute path instead of being relative to the project:
System.IO.DirectoryNotFoundException: "Could not find a part of the path 'C:\Assets\Logos\logo.png'."
I set build action to AvaloniaResource
so it should be included in my assembly. Also tried <x:String>Assets/Logos/ul-logo.png</x:String>
, now the exception refers to the debug folder (bin/Debug/net5.0/Assets
).
How can I specify a resource that just holds the /Assets/Logos/logo.png
path and resolve it as hard-coded paths in the <Image>
element would do?