2

I try to define an ImageSource with AvaloniaUi. In WPF I was going this way:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
   <ImageSource x:Key="Icon">path/to/image/image.png</ImageSource>
</ResourceDictionary>

And then referenced to it like this:

<Image Source="{StaticResource Icon}"/>

How can i archive the same in Avalonia?

Relaxo
  • 75
  • 1
  • 9

1 Answers1

2
<UserControl xmlns="https://github.com/avaloniaui"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:imaging="clr-namespace:Avalonia.Media.Imaging;assembly=Avalonia.Visuals">
    <UserControl.Resources>
        <imaging:Bitmap x:Key="MyBitmap">
            <x:Arguments><x:String>icon.png</x:String></x:Arguments>
        </imaging:Bitmap>
    </UserControl.Resources>
       <Image Source="{StaticResource MyBitmap}"
               Width="100" Height="200"
               Stretch="None"/>
</UserControl>

Note that this only works with physical paths, since converter isn't involved anymore.

You can also try to experiment with attached properties that will accept your own image holder.

kekekeks
  • 3,193
  • 19
  • 16
  • This works, thank you for the fast answer. I put the imaging:Bitmap in a seperate .XAML file, inside a Style.Resource which holds all Images together. – Relaxo Oct 10 '18 at 14:09