vixez
vixez

Reputation: 857

Inline Hyperlink navigate to Page

I'm constructing text, and some pieces of the text should contain a hyperlink. However, these hyperlinks do not redirect to a webpage but should open a page in the UWP app (the currently running UWP app, not a new instance of it or a different app).

A HyperlinkButton can only open URL's that lead to an external browser, it can't open a page inside the app.

Using an InlineUIContainer doesn't work, I get

Exception thrown: 'System.ArgumentException' in mscorlib.ni.dll Additional information: Value does not fall within the expected range.

With this code

List<Inline> ic = new List<Inline>();
InlineUIContainer container = new InlineUIContainer();
TextBlock tbClickable = new TextBlock();
tbClickable.Foreground = new SolidColorBrush(Colors.Red);
tbClickable.Text = label?.name;
tbClickable.Tag = label?.id;
tbClickable.Tapped += TbArtist_Tapped;
container.Child = tbClickable;
ic.Add(container);

When I use

foreach (var item in ic)
{
    dTb.Inlines.Add(item);
}

Where tbCurrent is the TextBlock.

Any other ways to get a clickable link as an Inline element? Best case scenario I can attach a Tapped/Click event handler. But opening the page via a URI method or so is also good.

Upvotes: 1

Views: 477

Answers (2)

vixez
vixez

Reputation: 857

I changed to a RichTextBlock and using Blocks I could add a clickable TextBlock. This works in UWP.

List<Block> ic = new List<Block>();
Paragraph para = new Paragraph();
InlineUIContainer iuic = new InlineUIContainer();
TextBlock hpb = new TextBlock();
hpb.Text = "link text";
hpb.Tag = "some tag to pass on to the click handler";
hpb.Tapped += ClickHandler;
hpb.TextDecorations = TextDecorations.Underline;
hpb.Foreground = new SolidColorBrush((Windows.UI.Color)page.Resources["SystemAccentColor"]);
iuic.Child = hpb;

para.Inlines.Add(iuic);
ic.Add(para);

Upvotes: 1

Martin Zikmund
Martin Zikmund

Reputation: 39082

There is no reason you could not use a HyperlinkButton for this. Quick example:

<HyperlinkButton Click="HyperlinkButton_Click" Content="Click me" />

And the Click handler:

private void HyperlinkButton_Click(object sender, RoutedEventArgs e)
{
    Frame.Navigate(typeof(MainPage));
}

Upvotes: 0

Related Questions