本主題演示了如何檢測絕對 URI 以及如何在引用應用包中的資源時創建壹個。
檢測和使用絕對 URI
使用 Uri.IsAbsoluteUri 屬性確保在將 URI 傳遞給 Windows 運行時 之前,該 URI 是絕對的。 使用此屬性比捕獲和處理ArgumentException 異常更為有效。
將絕對 URI 用於應用包中的資源
如果想要為應用包包含的資源指定 URI,可以使用 ms-appx 或 ms-appx-web 方案來創建絕對 URI。
下面的示例演示如何同時使用 XAML 和代碼,將 WebView 控件的 Source 屬性以及 Image 控件的 Source 屬性設置為包含名為 Pages 文件夾中的資源。
C#
VB
private void Button_Click_1(object sender, RoutedEventArgs e)
{
webview1.Source = new Uri("ms-appx-web:///Pages/HTMLPage2.html", UriKind.Absolute);
}
XAML
<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>