简体   繁体   English

TextBlock中的HyperLinkBut​​ton

[英]HyperLinkButton in TextBlock

I have the following XAML code: 我有以下XAML代码:

<TextBlock 
    FontFamily="Segoe UI Light"
    TextWrapping="Wrap" Grid.Row="0"
    Margin="12" TextAlignment="Center"
    SelectionHighlightColor="#FF1D1334"
    FontWeight="Light" Foreground="#FF755CB0">
    <Span FontSize="40" >Welcome!</Span><LineBreak/><LineBreak/>
    <Span FontSize="25"> login</Span>
    <LineBreak/>
    click here
    <InlineUIContainer>
        <HyperlinkButton 
            Foreground="#FF4017A0"
            Click="RedirectToRegister">HERE!</HyperlinkButton>
    </InlineUIContainer>
</TextBlock>

The problem is that I got this error: 问题是我得到了这个错误:

Error 1 Could not find Windows Runtime type 'Windows.UI.Xaml.Controls.RelativeSource'. 错误1找不到Windows运行时类型'Windows.UI.Xaml.Controls.RelativeSource'。

How can I put the hyperLinkButton inside my paragraph? 如何将hyperLinkBut​​ton放在我的段落中?

I tried ReachTextBlock, but I don't want the text to be selected. 我尝试了ReachTextBlock,但是我不想选择文本。

Wish for help, thanks 希望获得帮助,谢谢

I don't understand the error message, but I was able to reproduce it and other error messages (they varied as I tried different things…the most persistent error message was actually a complaint that the ButtonBase class couldn't be found). 我不理解该错误消息,但是我能够重现该错误消息和其他错误消息(当我尝试不同的操作时,它们会有所不同……最持久的错误消息实际上是抱怨找不到ButtonBase类)。

I don't have a good explanation for them; 我对他们没有很好的解释; it seems like at the very least there's a bug in the XAML compiler with respect to emitting useful error messages. 似乎至少在XAML编译器中存在一个与发出有用错误消息有关的错误。 I suppose there's something wrong with the code you tried to write, but a) don't see immediately what that might be, and b) the XAML compiler should give you an actionable error message, rather than complaining it can't find a type that clearly is present in your project references. 我想您尝试编写的代码出了点问题,但是a)无法立即看到可能是什么,并且b)XAML编译器应该为您提供可操作的错误消息,而不是抱怨它找不到类型显然存在于您的项目参考中。

All that said, you can achieve what you want without the InlineUIContainer class (I presume you inherited that technique from WPF?). InlineUIContainer无需InlineUIContainer类就可以实现所需的InlineUIContainer (我想您是从WPF继承了该技术的?)。 The following code should work for you: 以下代码将为您工作:

<TextBlock 
    FontFamily="Segoe UI Light"
    TextWrapping="Wrap" Grid.Row="0"
    Margin="12" TextAlignment="Center"
    SelectionHighlightColor="#FF1D1334"
    FontWeight="Light" Foreground="#FF755CB0">
    <Span FontSize="40" >Welcome!</Span><LineBreak/><LineBreak/>
    <Span FontSize="25"> login</Span>
    <LineBreak/>
    click here
    <Hyperlink Click="RedirectToRegister" Foreground="#FF4017A0">HERE!</Hyperlink>
</TextBlock>

Note that you may also want to adjust the method signature for your RedirectToRegister() method. 请注意,您可能还需要调整RedirectToRegister()方法的方法签名。 Eg: 例如:

private void RedirectToRegister(
    Windows.UI.Xaml.Documents.Hyperlink sender,
    Windows.UI.Xaml.Documents.HyperlinkClickEventArgs args)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM