Информация об изменениях

Сообщение Re[7]: WebView2 с Proxy от 16.07.2025 13:16

Изменено 16.07.2025 14:49 Passerby

Re[7]: WebView2 с Proxy
Здравствуйте, bnk, Вы писали:

bnk>У меня он работает без прокси

Кто провайдер?

bnk>Ну я и говорю, ты уже где-то инициализируешь его до Form_Load (ставишь Source?)

Нет, Source не ставлю.
bnk>Ты сделал как написано выше, по шагам (1, 2)?
Сделал снова.
Новое приложение Form от Microsoft. Или нужен с FrameWork? Net 9.0.
Установил в NuGet WebView2.
Дополнил конструктор Form1.
К событию Load подсоединил метод Form1_Load(object sender, EventArgs e).
Запустил отладку, ошибка на строке await myWebView2.EnsureCoreWebView2Async(env);
using Microsoft.Web.WebView2.Core;
//using Microsoft.Web.WebView2.WinForms;
namespace WinFormsApp1
{
    public partial class Form1 : Form
    {
        //private readonly WebView2 myWebView2 = new WebView2(); уже есть определение
        public Form1()
        {
            InitializeComponent();
            myWebView2.CreationProperties = null;
            myWebView2.Location = new Point(10, 65);
            myWebView2.Name = "BingWebView";
            myWebView2.Size = new Size(1750, 780);
            myWebView2.Source = new Uri("https://www.bing.com/", UriKind.Absolute);
            myWebView2.TabIndex = 0;
            myWebView2.ZoomFactor = 1D;
            myWebView2.Visible = true;
            this.SuspendLayout();
            ClientSize = new Size(1800, 800);
            Controls.Add(myWebView2);
            Name = "Form1";
            Text = "Form1";
            ResumeLayout(false);
        }
        private async void Form1_Load(object sender, EventArgs e)
        {
            CoreWebView2EnvironmentOptions Options = new CoreWebView2EnvironmentOptions();
            Options.AdditionalBrowserArguments = "--proxy-server=http://1.2.3.4:8888";
            CoreWebView2Environment env =
                await CoreWebView2Environment.CreateAsync(null, null, Options);
            await myWebView2.EnsureCoreWebView2Async(env);
        }
    }
}

namespace WinFormsApp1
{
    partial class Form1
    {
        /// <summary>
        ///  Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        ///  Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        ///  Required method for Designer support - do not modify
        ///  the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            myWebView2 = new Microsoft.Web.WebView2.WinForms.WebView2();
            ((System.ComponentModel.ISupportInitialize)myWebView2).BeginInit();
            SuspendLayout();
            // 
            // webView21
            // 
            myWebView2.AllowExternalDrop = true;
            myWebView2.CreationProperties = null;
            myWebView2.DefaultBackgroundColor = Color.White;
            myWebView2.Location = new Point(12, 52);
            myWebView2.Name = "webView21";
            myWebView2.Size = new Size(776, 372);
            myWebView2.TabIndex = 0;
            myWebView2.ZoomFactor = 1D;
            // 
            // Form1
            // 
            AutoScaleDimensions = new SizeF(7F, 15F);
            AutoScaleMode = AutoScaleMode.Font;
            ClientSize = new Size(800, 450);
            Controls.Add(myWebView2);
            Name = "Form1";
            Text = "Form1";
            Load += Form1_Load;
            ((System.ComponentModel.ISupportInitialize)myWebView2).EndInit();
            ResumeLayout(false);
        }

        #endregion

        private Microsoft.Web.WebView2.WinForms.WebView2 myWebView2;
    }
}
Re[7]: WebView2 с Proxy