XPS suppress navigateuri error for custom URL

Pino Carafa 121 Reputation points
2021-07-12T08:17:26.593+00:00

We are trying to view an XPS document using a Document Viewer as follows ( VB.NET code )

oDoc = New Xps.Packaging.XpsDocument(path:=sDocument, packageAccess:=IO.FileAccess.Read)
oSequence = oDoc.GetFixedDocumentSequence
oDocViewer.Document = oSequence

The second line causes it to throw an error
System.Windows.Markup.XamlParseException: ''Failed to create a 'NavigateUri' from the text .....

With an inner exception
Invalid URI: The hostname could not be parsed.

The URI in question, however, is a custom one, something like:
mylittlecustomprotocol://tonsandtonsofencryptedgarbagehere

The protocol handler we wrote for this has no problem with it. So we were hoping that there is a way to suppress this error for the mylittlecustomprotocol protocol

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,800 questions
{count} vote

1 answer

Sort by: Most helpful
  1. Hui Liu-MSFT 48,586 Reputation points Microsoft Vendor
    2021-07-16T06:35:51.443+00:00

    For self-generated xps files with custom hyperlinks, you need to add RequestNavigate to the hyperlinks. Then you can navigate the hyperlink to your target file.
    The code of the project that generates the xps file:

    Imports System.IO  
    Imports System.IO.Packaging  
    Imports System.Windows.Xps.Packaging  
    Imports System.Windows.Xps.Serialization  
      
    Partial Public Class MainWindow  
        Inherits Window  
      
        Public Sub New()  
            InitializeComponent()  
        End Sub  
      
        Private Sub Hyperlink_RequestNavigate(ByVal sender As Object, ByVal e As System.Windows.Navigation.RequestNavigateEventArgs)  
            Process.Start(New ProcessStartInfo(e.Uri.AbsoluteUri))  
            e.Handled = True  
        End Sub  
      
        Private Sub Window_Loaded(ByVal sender As Object, ByVal e As RoutedEventArgs)  
            Dim myParagraph As Paragraph = New Paragraph()  
            Dim run1 As Run = New Run("mylittlecustomprotocol://tonsandtonsofencryptedgarbagehere")  
            Dim h1 As Hyperlink = New Hyperlink(run1)  
            h1.NavigateUri = New Uri("mylittlecustomprotocol://tonsandtonsofencryptedgarbagehere")  
            AddHandler h1.RequestNavigate, AddressOf Hyperlink_RequestNavigate  
      
            myParagraph.Inlines.Add(h1)  
            Dim myFlowDocument As FlowDocument = New FlowDocument()  
            myFlowDocument.Blocks.Add(myParagraph)  
            Me.Content = myFlowDocument  
            Dim ftx As FlowToXps = New FlowToXps()  
            ftx.SaveAsXps("C:\\...\\tt.xps", myFlowDocument)  
        End Sub  
    End Class  
      
    Public Class FlowToXps  
        Public Sub SaveAsXps(ByVal path As String, ByVal document As FlowDocument)  
            Using package As Package = Package.Open(path, FileMode.Create)  
      
                Using xpsDoc = New XpsDocument(package, CompressionOption.Maximum)  
                    Dim xpsSm = New XpsSerializationManager(New XpsPackagingPolicy(xpsDoc), False)  
                    Dim dp As DocumentPaginator = (CType(document, IDocumentPaginatorSource)).DocumentPaginator  
                    xpsSm.SaveAsXaml(dp)  
                End Using  
            End Using  
        End Sub  
    End Class  
    

    The code of the project that reads the xps file:

    Imports System.IO  
    Imports System.Windows.Xps.Packaging  
    Partial Public Class MainWindow  
        Public Sub New()  
            InitializeComponent()  
            Dim ogXps = "C:\\...\\tt.xps"  
            Dim xpsDoc As XpsDocument = New XpsDocument(ogXps, FileAccess.Read)  
            Dim seqDoc As FixedDocumentSequence = xpsDoc.GetFixedDocumentSequence()  
            dv.Document = seqDoc  
        End Sub  
    End Class  
    

    The result is shown in the figure:
    115228-11.png


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.