簡體   English   中英

WebView2: System.ArgumentException: '值不在預期范圍內。'

[英]WebView2: System.ArgumentException: 'Value does not fall within the expected range.'

嘗試調用 webview2.CoreWebView2.Navigate(html) 時出現異常...代碼如下:

在此處輸入圖像描述

<Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:wv2="clr-namespace:Microsoft.Web.WebView2.Wpf;assembly=Microsoft.Web.WebView2.Wpf"       
        xmlns:local="clr-namespace:WpfApp1"
        mc:Ignorable="d"
        Loaded="Window_Loaded"
        Title="MainWindow" Height="450" Width="800">
    <DockPanel LastChildFill="True">
        <StackPanel Orientation="Horizontal"  DockPanel.Dock="Top" Background="Aqua">
            <TextBox Name="HtmlId" Width="80" Text="id6"/>
            <Button Name="Go" Content="Go" Click="Go_Click"/>
        </StackPanel>

        <wv2:WebView2 Name="webview2" 
          CoreWebView2InitializationCompleted
           ="webview2_CoreWebView2InitializationCompleted"  />

    </DockPanel>
</Window>
// WebView2 Install:
//    PM > Install - Package Microsoft.Web.WebView2
//    Add to XML: xmlns:wv2="clr-namespace:Microsoft.Web.WebView2.Wpf;assembly=Microsoft.Web.WebView2.Wpf"       
//    using Microsoft.Web.WebView2.Core;
using System;
using System.Windows;
using System.Windows.Controls;
using Microsoft.Web.WebView2.Core;

// WebView2 Install:
//    PM > Install - Package Microsoft.Web.WebView2
//    Add to XML: xmlns:wv2="clr-namespace:Microsoft.Web.WebView2.Wpf;assembly=Microsoft.Web.WebView2.Wpf"       
//    using Microsoft.Web.WebView2.Core;

namespace WpfApp1
{
    public partial class MainWindow : Window
    {
        string html = "";

        private async void Window_Loaded(object sender, RoutedEventArgs e)
        {
            await webview2.EnsureCoreWebView2Async();
        }

        private void webview2_CoreWebView2InitializationCompleted(
            object sender, CoreWebView2InitializationCompletedEventArgs e)
        {
            Console.WriteLine(html);
            webview2.CoreWebView2.Navigate(html);

        }

        public MainWindow()
        {
            InitializeComponent();

            html = html + "<ul>";
            for (int i = 0; i < 100; i++)
            {
                string id = "id" + i.ToString();
                html = html + "<li><p id=\"" + id 
                     + "\">this is id = " + id + " </p></li>\n";
            }
            html = html + "</ul>";

         }

        private void Go_Click(object sender, RoutedEventArgs e)
        {
        }
    }
}

需要將 Navigate() 更改為 NavigateToString()

        private void webview2_CoreWebView2InitializationCompleted(
           object sender, 
           CoreWebView2InitializationCompletedEventArgs e)
        {
            Console.WriteLine(html);
            webview2.CoreWebView2.NavigateToString(html);

        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM