@@ -2,38 +2,42 @@ CoreWebView2.CreateFromCOMObject
22===
33
44# Background
5- The new [ Unity WebView2] ( https://learn.microsoft.com/en-us/windows/mixed-reality/develop/advanced-concepts/webview2-unity-plugin )
6- control creates and uses C++ COM to create and manage the ICoreWebView2* objects. However,
7- Unity developers are often interacting with the Unity WebView2 control using C#/.NET. The Unity WebView2 control doesn't expose
8- the CoreWebView2 directly to devs using the Unity WebView2 control, so when devs want to call an API on CoreWebView2, they
9- have to rely on that API being exposed on the Unity WebView2 control, which then internally calls into CoreWebView2. This
10- is in contrast to our other controls (like WPF WebView2 and Winforms WebView2 controls) which directly give access to their [ CoreWebView2
11- object] ( https://learn.microsoft.com/en-us/dotnet/api/microsoft.web.webview2.winforms.webview2.corewebview2?view=webview2-dotnet-1.0.2088.41 ) ,
12- allowing devs to call any API that exists or gets added to the CoreWebView2/ICoreWebView2_ * . The Unity WebView2 control can't do this today,
13- as they are unable to create a CoreWebView2 object that wraps an already existing COM object. To help implement this for Unity,
14- we are adding a new static factory function on CoreWebView2 .NET class that will allow it to wrap an existing ICoreWebView2 COM object, instead
15- of creating a new one underlying ICoreWebView2.
5+ The new [ Unity WebView2] (https://learn.microsoft.com/en-us/windows/mixed-reality/develop/
6+ advanced-concepts/webview2-unity-plugin) control creates and uses C++ COM to create and manage the
7+ ICoreWebView2* objects. However, Unity developers are often interacting with the Unity WebView2
8+ control using C#/.NET. The Unity WebView2 control doesn't expose the CoreWebView2 directly to devs
9+ using the Unity WebView2 control, so when devs want to call an API on CoreWebView2, they
10+ have to rely on that API being exposed on the Unity WebView2 control, which then internally calls
11+ into CoreWebView2. This is in contrast to our other controls (like WPF WebView2 and Winforms
12+ WebView2 controls) which directly give access to their [ CoreWebView2 object]
13+ (https://learn.microsoft.com/en-us/dotnet/api/microsoft.web.webview2.winforms.webview2 .
14+ corewebview2?view=webview2-dotnet-1.0.2088.41), allowing devs to call any API that exists or gets
15+ added to the CoreWebView2/ICoreWebView2_ * . The Unity WebView2 control can't do this today,
16+ as they are unable to create a CoreWebView2 object that wraps an already existing COM object.
17+ To help implement this for Unity, we are adding a new static factory function on CoreWebView2 .NET
18+ class that will allow it to wrap an existing ICoreWebView2 COM object, instead of creating a new
19+ one underlying ICoreWebView2.
1620
1721# Examples
18- ## CoreWebView2.CreateFromCOMObject
22+ ## CoreWebView2.CreateFromComObject
1923``` c#
2024public class MyWebView2Control
2125{
2226 ... // Regular control code
2327
2428 // A previously created ICoreWebView2
25- IntPtr _myCoreWebView2COMObject = .. .;
29+ IntPtr _myCoreWebView2ComObject = .. .;
2630
2731 CoreWebView2 _myCoreWebView2 = null ;
2832
29- // THis is the CoreWebView2 property which allows developers to access CoreWebView2 APIs directly.
33+ // This is the CoreWebView2 property which allows developers to access CoreWebView2 APIs directly.
3034 public CoreWebView2 CoreWebView2
3135 {
3236 get
3337 {
3438 if (! _myCoreWebView2 )
3539 {
36- _myCoreWebView2 = CoreWebView2 .CreateFromCOMObject (_myCoreWebView2Object );
40+ _myCoreWebView2 = CoreWebView2 .CreateFromComObject (_myCoreWebView2Object );
3741 }
3842 return _myCoreWebView2 ;
3943 }
@@ -47,24 +51,30 @@ public class MyWebView2Control
4751``` c#
4852namespace Microsoft .Web .WebView2 .Core
4953{
50- runtimeclass CoreWebView2
54+ public class CoreWebView2
5155 {
5256 /// <summary >
5357 /// Creates a CoreWebView2 object that wraps an existing COM ICoreWebView2 object.
5458 /// This allows interacting with the WebView2 control using .NET,
5559 /// even if the control was originally created using COM.
5660 /// </summary >
57- /// <param name =" value" >Pointer to the COM object representing the ICoreWebView2 control .</param >
61+ /// <param name =" value" >Pointer to a COM object that implements the ICoreWebView2 COM interface .</param >
5862 /// <returns >Returns a .NET CoreWebView2 object that wraps the COM object.</returns >
5963 /// <exception cref =" ArgumentNullException" >Thrown when the provided COM pointer is null.</exception >
6064 /// <exception cref =" InvalidComObjectException" >Thrown when the value is not an ICoreWebView2 COM object and cannot be wrapped.</exception >
61- public static CoreWebView2 CreateFromCOMObject (IntPtr value);
65+ public static CoreWebView2 CreateFromComObject (IntPtr value );
6266 }
6367}
6468```
6569
6670# Appendix
67- We have a couple of other options to accomplish this, including moving the "CreateFromCOMOBject " function to the
71+ We have a couple of other options to accomplish this, including moving the "CreateFromComOBject " function to the
6872CoreWebView2Controller class instead. CoreWebView2Controller could then be used to get the CoreWebView2 through
6973it's CoreWebView2 property which already exists. Or we could expose a new constructor on CoreWebView2/CoreWebView2Controller,
7074instead of a factory method.
75+
76+ We decided on using the CoreWebView2 due to it being the class most likely to be exposed and used
77+ in .NET, and which is the same across different C# frameworks.
78+ We decided on a factory method to not give the impression that a new constructor is the default
79+ one (we don't currently have any public constructors), and to make the intent and usage of
80+ the method more obvious.
0 commit comments