[SOLVED] Open link in code behind

Please post public support tickets here. Note: for private support tickets, please send an email to support@cshtml5.com instead.
Greenman1805
Posts: 6
Joined: Sun Dec 24, 2017 3:24 am

[SOLVED] Open link in code behind

Postby Greenman1805 » Thu Jan 04, 2018 6:19 am

I tried to open a html link like this:
private void TextBlock_PointerPressed(object sender, PointerRoutedEventArgs e)
{
HtmlPresenter1.Html = " < meta http - equiv = "refresh" content = "5; URL = https://mylink.com/" >";
}

but it says that HtmlPresenter1 isn't available. I also tried "this.Html..." but it didn't work, so how is this possible? :roll:

TaterJuice
Posts: 147
Joined: Thu Mar 16, 2017 5:40 am
Contact:

Re: Open link in code behind

Postby TaterJuice » Thu Jan 04, 2018 6:37 am

What I've done is create a HyperlinkButton, set the NavigationUri property to "google.com" then invoke the OnClick event using Reflection.

Try this:

XAML:

Code: Select all

<Page
    x:Class="MyProject.Pages.ButtonClickPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:MyProject.Pages">
    <StackPanel>
        <Button
            Content="Go to Google"
            Click="Button_Click" />
    </StackPanel>
</Page>


Code Behind:

Code: Select all

using System;
using System.Reflection;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;

namespace MyProject.Pages
{
    public partial class ButtonClickPage : Page
    {
        public ButtonClickPage() { InitializeComponent(); }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var linkButton = new HyperlinkButton();
            linkButton.NavigateUri = new Uri("http://www.google.com", UriKind.RelativeOrAbsolute);
            typeof(ButtonBase).GetMethod("OnClick", BindingFlags.Instance | BindingFlags.NonPublic).Invoke(linkButton, new object[0]);
        }
    }
}

Greenman1805
Posts: 6
Joined: Sun Dec 24, 2017 3:24 am

Re: Open link in code behind

Postby Greenman1805 » Thu Jan 04, 2018 8:11 am

Thanks! I will try this.

JS-Support @Userware
Site Admin
Posts: 1142
Joined: Tue Apr 08, 2014 3:42 pm

Re: Open link in code behind

Postby JS-Support @Userware » Fri Jan 05, 2018 5:18 am

Hi,

To open link in code behind, you can also use the following code:

Code: Select all

HtmlPage.Window.Navigate(new Uri(@"https://mylink.com/"), "_blank");


Please note that the browser may require that this code is initiated by a user action, such as a click or another event.

Regards,
JS-Support

Greenman1805
Posts: 6
Joined: Sun Dec 24, 2017 3:24 am

Re: Open link in code behind

Postby Greenman1805 » Fri Jan 05, 2018 10:47 am

JS-Support wrote:Hi,

To open link in code behind, you can also use the following code:

Code: Select all

HtmlPage.Window.Navigate(new Uri(@"https://mylink.com/"), "_blank");


Please note that the browser may require that this code is initiated by a user action, such as a click or another event.

Regards,
JS-Support


Works too! Thanks!


Return to “Technical Support”

Who is online

Users browsing this forum: No registered users and 38 guests