项目作者: ITAgnesmeyer

项目描述 :
WebView2 Wrapper
高级语言: C#
项目地址: git://github.com/ITAgnesmeyer/Diga.WebView2.git
创建时间: 2020-04-08T14:36:26Z
项目社区:https://github.com/ITAgnesmeyer/Diga.WebView2

开源协议:MIT License

下载


Diga.WebView2

WebView2 Wrapper For => Edge Chromium

Microsoft WebView2

Nuget Packages:

There are NUGET-PACKAGES

  • Diga.WebView2.Interop => NuGet
  • Diga.WebView2.Wrapper => NuGet
  • Diga.WebView2.WinForms => NuGet
  • Diga.WebView2.Wpf => NuGet
  • Diga.WebView2.Scripting => NuGet

WebView2 Runtime

Windows Forms

If you use net Framework. You have to modify the Diga.WebView2.Interop.dll Reference.

Set Embed Interop Types to False!
This copies the DLL into the program directory. If you do not make this setting, there will be an error when starting the application.

WinForms is STAThread. The access to the component must therefore also be thread-safe, as is usual in WinForms. You should never call properties or functions of a component directly from a Task. Use a delegate to do this, if necessary.

  1. public void SendMessage(string message)
  2. {
  3. if (this.webView1.InvokeRequired)
  4. {
  5. Action<string> ac = SendMessage;
  6. this.webView1.Invoke(ac, message);
  7. }
  8. else
  9. {
  10. this.webView1.SendMessage(message);
  11. }
  12. }
  13. public async Task InvokeSendMessage(string msg)
  14. {
  15. await Task.Run(() => this.SendMessage(msg));
  16. }

DOM Objects

The Windows Forms and WPF project now supports DOM objects.
You can retrieve the DOM - Window object and the DOM - Document object directly by using the GetDOMWidow() and GetDOMDocument() functions.
Events are only add sync functions!!

  1. DOMElement button = this.WebView3.GetDOMDocument().getElementById("ga_button");
  2. button.Click += (o, ev) =>
  3. {
  4. MessageBox.Show("Test from Button");
  5. };

The following code will throw an exception!!

  1. DOMElement button = this.WebView3.GetDOMDocument().getElementById("ga_button");
  2. // you cannot add async Eventhandler!!!
  3. // this will Raise InvalidOperation!!
  4. button.Click += async (o, ev) =>
  5. {
  6. await this.WebView3.GetDOMWindow().alertAsync("TEST");
  7. };

Asynchronous and synchronous calls should never be mixed.
If possible, use the synchronous calls.

Correct example

  1. DOMDocument doc = this.webView1.GetDOMDocument();
  2. this._DIV = doc.createElement("div");
  3. this._DIV.id = Guid.NewGuid().ToString();
  4. doc.body.appendChild(this._DIV);
  5. //add a button element
  6. DOMElement element = doc.createElement("button");
  7. //set inner html of button
  8. element.innerHTML = "Click Me";
  9. //set id of Button-Element
  10. element.id = Guid.NewGuid().ToString();
  11. //add Button to DIV
  12. this._DIV.appendChild(element);
  13. //Add EventHandler
  14. // Important never call synchron Properties and Functions in an async function
  15. element.Click += OnDomElementClick1;
  16. element.Click += OnDomElementClick;
  17. ...
  18. private void OnDomElementClick1(object sender, DOMMouseEventArgs e)
  19. {
  20. ...
  21. }
  22. private void OnDomElementClick(object sender, DOMMouseEventArgs e)
  23. {
  24. ...
  25. }

Note that as soon as a new document is created, the old variables become invalid.
You can query the validity of the variable with VarExist().

Check validity of the object

  1. //sync
  2. if(!this._DIV.VarExist())
  3. this._DIV = nothing;
  4. ...
  5. // async
  6. bool varExist = await this._DIV.VarExistAsync();

Canvas

  1. //create Canvas
  2. DOMElement elem = this.WebView3.GetDOMDocument().createElement("canvas");
  3. this.WebView3.GetDOMDocument().body.appendChild(elem);
  4. DOMCanvasElement can = new DOMCanvasElement(elem);
  5. can.width = 200;
  6. can.height = 100;
  7. // Create Context
  8. var ctx = can.GetContext2D();
  9. //yellow rectangle
  10. ctx.fillStyle = "yellow";
  11. ctx.fillRect(0,0,255,255);
  12. //transform
  13. ctx.transform(1,(decimal)0.5, (decimal)-0.5,1,30,10);
  14. //blue rectangle
  15. ctx.fillStyle = "blue";
  16. ctx.fillRect(0,0,250,100);
  17. //reset Transform
  18. ctx.resetTransform();
  19. //draw line
  20. ctx.moveTo(0, 0);
  21. ctx.lineTo(200,100);
  22. ctx.stroke();