Skip to content

How to customise linkaction

whistyun edited this page Oct 24, 2021 · 1 revision

Built-in action

In default, MarkdownScrollViewer act nothing when an user clicks the link.

MarkdownScrollViewer.ClickAction property change action when clicked link.

<mdxam:MarkdownScrollViewer
        ClickAction="DisplayWithRelativePath"
        />
ClickAction Description
None act nothing
OpenBrowser This behaves like Win+r. If link starts with 'http://', the webbrowser open the link.
DisplayWithRelativePath If the link is the relative path, The link destination is opened as markdown and displayed. If the link is the absolute path, This bahaves like OpenBrowser.
DisplayAll The link destination is opened as markdown and displayed.

Full customise

CustomLinkCommand.cs

class CustomLinkCommand : ICommand
{
    public event EventHandler CanExecuteChanged;

    public bool CanExecute(object parameter) => true;

    public void Execute(object parameter)
    {
        var href = (string)parameter;

        try
        {
            Process.Start(new ProcessStartInfo(href)
            {
                UseShellExecute = true,
                Verb = "open"
            });
        }
        catch
        {
            // error handle; notifications, path changes, etc. 
            // MessageBox.Show($"Failed to open {href}");
        }
    }
}

Page.xaml

<mdxam:Markdown x:Key="MdEngine">
    <mdxam:Markdown.HyperlinkCommand>
        <local:CustomLinkCommand/>
    </mdxam:Markdown.HyperlinkCommand>
</mdxam:Markdown>

...

<xaml:MarkdownScrollViewer
    Style="{StaticResource MarkdownScrollViewerStyle}"
    Markdown="{Binding Text}"
    Engine="{StaticResource MdEngine}" />
    <!-- ClickAction settings need to be removed -->
Clone this wiki locally