Skip to content

How to customise loder

whistyun edited this page Jul 8, 2023 · 3 revisions

Warning
This features is deprecated from 11.0.0-d2. Please see How-to-customise-imagepath-resolving instead.

Abstract

Markdown.Avalonia.Markdown class has BitmapLoader property.

BitmapLoader property is Markdown.Avalonia.Utils.IBitmapLoader type allow customise how to load bitmap.

Sample

Xaml

<!-- xmlns:md="clr-namespace:Markdown.Avalonia;assembly=Markdown.Avalonia" -->

<Window.Resources>
    <local:MyBitmapLoader x:Key="myImageLoader"/>
</Window.Resources>

...

<md:MarkdownScrollViewer>
    <md:MarkdownScrollViewer.Engine>
        <md:Markdown
            BitmapLoader="{StaticResource myImageLoader}"
            />
    </md:MarkdownScrollViewer.Engine>
</md:MarkdownScrollViewer>

MyBitmapLoader.cs

// using Markdown.Avalonia.Utils;

public class MyBitmapLoader : IBitmapLoader
{
    // local file root path, the default is current directory.
	public string AssetPathRoot { set; private get; }

    // The url of the image described in markdown is passed to urlTxt as it is.
    // relative path remains relative path, absolute path remain absolute path.
	public Bitmap Get(string urlTxt)
	{
		// load any image.
		// If you return null, Markdown.Avalonia considers it a load failure
		// and displays an alternative error image.
		return null;
	}
}