|
| 1 | +CoreWebView2.CreateFromCOMObject |
| 2 | +=== |
| 3 | + |
| 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. |
| 16 | + |
| 17 | +# Examples |
| 18 | +## CoreWebView2.CreateFromCOMObject |
| 19 | +```c# |
| 20 | +public class MyWebView2Control |
| 21 | +{ |
| 22 | + ... // Regular control code |
| 23 | +
|
| 24 | + // A previously created ICoreWebView2 |
| 25 | + IntPtr _myCoreWebView2COMObject = ...; |
| 26 | + |
| 27 | + CoreWebView2 _myCoreWebView2 = null; |
| 28 | + |
| 29 | + // THis is the CoreWebView2 property which allows developers to access CoreWebView2 APIs directly. |
| 30 | + public CoreWebView2 CoreWebView2 |
| 31 | + { |
| 32 | + get |
| 33 | + { |
| 34 | + if (!_myCoreWebView2) |
| 35 | + { |
| 36 | + _myCoreWebView2 = CoreWebView2.CreateFromCOMObject(_myCoreWebView2Object); |
| 37 | + } |
| 38 | + return _myCoreWebView2; |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | +} |
| 43 | +``` |
| 44 | + |
| 45 | + |
| 46 | +# API Details |
| 47 | +```c# |
| 48 | +namespace Microsoft.Web.WebView2.Core |
| 49 | +{ |
| 50 | + runtimeclass CoreWebView2 |
| 51 | + { |
| 52 | + /// <summary> |
| 53 | + /// Creates a CoreWebView2 object that wraps an existing COM ICoreWebView2 object. |
| 54 | + /// This allows interacting with the WebView2 control using .NET, |
| 55 | + /// even if the control was originally created using COM. |
| 56 | + /// </summary> |
| 57 | + /// <param name="value">Pointer to the COM object representing the ICoreWebView2 control.</param> |
| 58 | + /// <returns>Returns a .NET CoreWebView2 object that wraps the COM object.</returns> |
| 59 | + /// <exception cref="ArgumentNullException">Thrown when the provided COM pointer is null.</exception> |
| 60 | + /// <exception cref="InvalidComObjectException">Thrown when the COM object cannot be wrapped.</exception> |
| 61 | + public static CoreWebView2 CreateFromCOMObject(IntPtr value); |
| 62 | + } |
| 63 | +} |
| 64 | +``` |
| 65 | + |
| 66 | +# Appendix |
| 67 | +We have a couple of other options to accomplish this, including moving the "CreateFromCOMOBject" function to the |
| 68 | +CoreWebView2Controller class instead. CoreWebView2Controller could then be used to get the CoreWebView2 through |
| 69 | +it's CoreWebView2 property which already exists. Or we could expose a new constructor on CoreWebView2/CoreWebView2Controller, |
| 70 | +instead of a factory method. |
0 commit comments