C# 向RichTextBlock内的超链接添加交互行为

C# 向RichTextBlock内的超链接添加交互行为,c#,xaml,uwp,win-universal-app,C#,Xaml,Uwp,Win Universal App,我有一个带有超链接的RichTextBlock 单击超链接时,我想从我的ViewModel执行命令,因此我使用了Microsoft.Xaml.Behaviors中的交互行为: <RichTextBlock> <Paragraph> <Hyperlink> <interactivity:Interaction.Behaviors> <core:EventTrigge

我有一个带有超链接的
RichTextBlock

单击超链接时,我想从我的ViewModel执行
命令
,因此我使用了Microsoft.Xaml.Behaviors中的交互
行为

<RichTextBlock>
    <Paragraph>
        <Hyperlink>
            <interactivity:Interaction.Behaviors>
                <core:EventTriggerBehavior EventName="Click">
                    <core:InvokeCommandAction
                        Command="{Binding ShowDocumentCommand}" />
                </core:EventTriggerBehavior>
            </interactivity:Interaction.Behaviors>
            <Hyperlink.Inlines>
                <Run Text="Some link" />
            </Hyperlink.Inlines>
        </Hyperlink>
    </Paragraph>
</RichTextBlock>

但它不起作用。它运行,但什么也没发生。单击
超链接时不执行该命令

为什么??
我应该怎么做才能让它执行命令?

看起来行为只能附加到从FrameworkElement派生的类。但超链接并不从中继承

您只需使用from UWP Community Toolkit包,该包已经具有所需的附加属性
Command
CommandParameter
。或者你可以直接复制粘贴他们的代码

因此,您的代码将如下所示:

<RichTextBlock>
    <Paragraph>
        <Hyperlink xaml:HyperlinkExtensions.Command="{Binding ShowDocumentCommand}">
            <Hyperlink.Inlines>
                <Run Text="Some link" />
            </Hyperlink.Inlines>
        </Hyperlink>
    </Paragraph>
</RichTextBlock>


看起来行为只能附加到从FrameworkElement派生的类。但是超链接不是从它继承的。好的。那么,除了在代码中添加处理程序之外,还有什么好方法可以从单击中调用命令呢?请参阅下面的答案。