从应用程序启动本机Web浏览器时出现访问冲突异常

时间:2013-10-10 16:58:17

标签: c# windows-8 winrt-xaml windows-applications windows-8.1

在我的C#WinRT应用程序中,我有一个userControl用于设置,其中包含一个打开本地html文件的webview。我使用javascript代码拦截此html文件上的链接以启动本机webbrowser。当我这样做时,本机webbrowser正确显示链接,但我的应用程序同时崩溃并出现“访问冲突”异常。

以下是我的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>

你有什么想法为什么会像这样崩溃?

非常感谢。

1 个答案:

答案 0 :(得分:0)

您必须使用Windows 8.1。

在Windows 8.1中,不允许使用AllowedScriptNotifyUris,而是在项目“Package.appxmanifest”中的“内容URI”中输入URL。

注意:提供的网址必须为“https”(http无效)。