Export (0) Print
Expand All
2 out of 13 rated this helpful - Rate this topic

Passing a URI to the Windows Runtime

.NET Framework 4.5

When you use the Windows Runtime in .NET Framework code, the Windows.Foundation.Uri class appears as System.Uri. For example, when you call a Windows Runtime method that takes a Uniform Resource Identifier (URI), IntelliSense displays the parameter type as System.Uri. The System.Uri class allows relative URIs, but the Windows.Foundation.Uri class does not. If you pass a relative URI to a Windows Runtime method, an ArgumentException exception is thrown in your code.

Note Note

This is also true for methods you expose in Windows Runtime Components. If your component exposes a method that takes a URI, the signature in your code includes System.Uri. However, to users of your component, the signature includes Windows.Foundation.Uri. A URI that is passed to your component must be an absolute URI.

Use the Uri.IsAbsoluteUri property to ensure that a URI is absolute before passing it to the Windows Runtime. Using this property is more efficient than catching and handling the ArgumentException exception.

If you want to specify a URI for a resource that your app package contains, you can use the ms-appx or ms-appx-web scheme to create an absolute URI.

The following example shows how to set the Source property for a WebView control and the Source property for an Image control to resources that are contained in a folder named Pages, using both XAML and code.

Private Sub Button_Click_1(sender As Object, e As RoutedEventArgs)
    webview1.Source = New Uri("ms-appx-web:///Pages/HTMLPage2.html", UriKind.Absolute)
End Sub
    <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
        <WebView Name="webview1" HorizontalAlignment="Center" Height="222"  
                 VerticalAlignment="Top" Width="310" Margin="472,57,553,0"
                 Source="ms-appx-web:///Pages/HTMLPage1.html"/>
        <Button Content="Button" HorizontalAlignment="Left" Margin="322,185,0,0" 
                VerticalAlignment="Top" Click="Button_Click_1"/>
	<Image HorizontalAlignment="Left" Height="100" Margin="208,123,0,0" VerticalAlignment="Top" 
               Width="100" Source="ms-appx:///Pages/weather.jpg" />

    </Grid>

For more information about these schemes, see URI schemes in the Windows Dev Center.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.