Tal
Tal

Reputation: 355

HyperLinkButton in TextBlock

I have the following XAML code:

<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'.

How can I put the hyperLinkButton inside my paragraph?

I tried ReachTextBlock, but I don't want the text to be selected.

Wish for help, thanks

Upvotes: 1

Views: 1093

Answers (1)

Peter Duniho
Peter Duniho

Reputation: 70691

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).

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. 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.

All that said, you can achieve what you want without the InlineUIContainer class (I presume you inherited that technique from 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. E.g:

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

Upvotes: 1

Related Questions