@@ -2,38 +2,42 @@ CoreWebView2.CreateFromCOMObject
2
2
===
3
3
4
4
# 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.
16
20
17
21
# Examples
18
- ## CoreWebView2.CreateFromCOMObject
22
+ ## CoreWebView2.CreateFromComObject
19
23
``` c#
20
24
public class MyWebView2Control
21
25
{
22
26
... // Regular control code
23
27
24
28
// A previously created ICoreWebView2
25
- IntPtr _myCoreWebView2COMObject = .. .;
29
+ IntPtr _myCoreWebView2ComObject = .. .;
26
30
27
31
CoreWebView2 _myCoreWebView2 = null ;
28
32
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.
30
34
public CoreWebView2 CoreWebView2
31
35
{
32
36
get
33
37
{
34
38
if (! _myCoreWebView2 )
35
39
{
36
- _myCoreWebView2 = CoreWebView2 .CreateFromCOMObject (_myCoreWebView2Object );
40
+ _myCoreWebView2 = CoreWebView2 .CreateFromComObject (_myCoreWebView2Object );
37
41
}
38
42
return _myCoreWebView2 ;
39
43
}
@@ -47,24 +51,30 @@ public class MyWebView2Control
47
51
``` c#
48
52
namespace Microsoft .Web .WebView2 .Core
49
53
{
50
- runtimeclass CoreWebView2
54
+ public class CoreWebView2
51
55
{
52
56
/// <summary >
53
57
/// Creates a CoreWebView2 object that wraps an existing COM ICoreWebView2 object.
54
58
/// This allows interacting with the WebView2 control using .NET,
55
59
/// even if the control was originally created using COM.
56
60
/// </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 >
58
62
/// <returns >Returns a .NET CoreWebView2 object that wraps the COM object.</returns >
59
63
/// <exception cref =" ArgumentNullException" >Thrown when the provided COM pointer is null.</exception >
60
64
/// <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 );
62
66
}
63
67
}
64
68
```
65
69
66
70
# 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
68
72
CoreWebView2Controller class instead. CoreWebView2Controller could then be used to get the CoreWebView2 through
69
73
it's CoreWebView2 property which already exists. Or we could expose a new constructor on CoreWebView2/CoreWebView2Controller,
70
74
instead 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