在 C# 中的 WinRT 应用程序中,我有一个用于设置的 userControl,其中包含一个打开本地 html 文件的 web 视图。我用 javascript 代码截取了这个 html 文件上的链接以启动本机网络浏览器。当我这样做时,本机网络浏览器会正确显示链接,但我的应用程序同时崩溃并出现“访问冲突”异常。
这是我的 UserControl 的代码:
public sealed partial class SimpleSettingsNarrow : UserControl
{
public SimpleSettingsNarrow()
{
this.InitializeComponent();
}
private void ConditionWB_Loaded_1(object sender, RoutedEventArgs e)
{
ConditionWB.ScriptNotify += ConditionWB_ScriptNotify;
ConditionWB.AllowedScriptNotifyUris = WebView.AnyScriptNotifyUri;
ConditionWB.Navigate(new Uri("ms-appx-web:///Assets/testWebview.html"));
}
private async void ConditionWB_ScriptNotify(object sender, NotifyEventArgs e)
{
await Windows.System.Launcher.LaunchUriAsync(new Uri(e.Value));
}
}
这是 xaml 代码:
<UserControl
x:Class="MyNameSpace.SimpleSettingsNarrow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:MyNameSpace"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="768"
d:DesignWidth="692">
<UserControl.Resources>
</UserControl.Resources>
<Border BorderBrush="Black" BorderThickness="1,0,0,0">
<Grid Background="White" VerticalAlignment="Stretch">
<!-- Root grid definition -->
<Grid.RowDefinitions>
<RowDefinition Height="80" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<!-- Header area for panel -->
<Grid Background="White" Grid.Row="0">
<Grid Margin="40,32,17,13">
<Grid.Transitions>
<TransitionCollection>
<EntranceThemeTransition FromHorizontalOffset="50" />
</TransitionCollection>
</Grid.Transitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="30" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<TextBlock Margin="10,0,0,0" Grid.Column="1" FontFamily="Segoe UI" FontWeight="Normal" FontSize="24.6667" Text="Déclaration de confidentialité" Foreground="Black" TextWrapping="Wrap" HorizontalAlignment="Left" />
</Grid>
</Grid>
<ScrollViewer Grid.Row="1" Margin="20,20,0,20" Padding="0,0,20,0">
<WebView x:Name="ConditionWB" Loaded="ConditionWB_Loaded_1"></WebView>
</ScrollViewer>
</Grid>
</Border>
</UserControl>
testWebview.html 文件包含:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xml:lang="fr" xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body>
test
<a href="javascript:window.external.notify('http://www.google.fr')">google</a>
</body>
</html>
你知道为什么它会这样崩溃吗?
非常感谢。