Skip to content

How to customise linkaction

whistyun edited this page Jan 3, 2021 · 1 revision

Abstract

Markdown.Avalonia.Markdown class has HyperlinkCommand property.

HyperlinkCommand property is ICommand type allow customise action when user click hyperlink.

Sample

Xaml

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

<Window.Resources>
    <local:MyLinkCommand x:Key="myLinkCommand"/>
</Window.Resources>

...

<md:MarkdownScrollViewer>
    <md:MarkdownScrollViewer.Engine>
        <md:Markdown
	        HyperlinkCommand="{StaticResource myLinkCommand}"
            />
    </md:MarkdownScrollViewer.Engine>
</md:MarkdownScrollViewer>

MyLinkCommand.cs

public class MyLinkCommand : ICommand
{
	public event EventHandler CanExecuteChanged;

	public bool CanExecute(object parameter) => true;

    // parameter is only string.
    // The url described in markdown is passed to parameter as it is.
    // relative path remains relative path, absolute path remain absolute path.
	public void Execute(object parameter)
	{
		var urlTxt = (string)parameter;

		Markdown.Avalonia.Utils.DefaultHyperlinkCommand.GoTo("http://example.com");
	}
}