I need to open a local .HTM file and navigate to a specific anchor name.
In this case it is an alarm information file with over 1,000 alarms / anchors.
In my test example (full code below) the Uri Fragment doesn't make it into the browser.
I have tried other ways of creating the hyperlink but this is as close as I could get.
MainWindow.xaml
<Window x:Class="HyperlinkWithPageAnchor.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="100" Width="200">
<Grid>
<TextBlock HorizontalAlignment="Center" VerticalAlignment="Center">
<Hyperlink NavigateUri="{Binding HyperlinkNavUri}" RequestNavigate="Hyperlink_RequestNavigate">
<TextBlock Text="Link Text"/>
</Hyperlink>
</TextBlock>
</Grid>
</Window>
MainWindow.xaml.cs
namespace HyperlinkWithPageAnchor
{
using System;
using System.Windows;
using System.ComponentModel;
using System.Windows.Navigation;
public partial class MainWindow : Window, INotifyPropertyChanged
{
private Uri _hyperlinkNavUri;
public Uri HyperlinkNavUri
{
get { return _hyperlinkNavUri; }
set { _hyperlinkNavUri = value; OnPropertyChanged(nameof(HyperlinkNavUri)); }
}
public MainWindow()
{
InitializeComponent(); DataContext = this;
// Desired Address: file:///C:/OSP-P/P-MANUAL/MPA/ENG/ALARM-A.HTM#1101
UriBuilder uBuild = new UriBuilder(new Uri("file://"));
uBuild.Path = @"C:\OSP-P\P-MANUAL\MPA\ENG\ALARM-A.HTM";
uBuild.Fragment = "1101";
HyperlinkNavUri = uBuild.Uri;
}
private void Hyperlink_RequestNavigate(object sender, RequestNavigateEventArgs e)
{
try { string link = e.Uri.ToString(); MessageBox.Show(link); System.Diagnostics.Process.Start(link); }
catch (Exception ex) { MessageBox.Show(ex.Message); }
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string name) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name)); }
}
}