forked from MicrosoftEdge/WebView2Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BridgeAddRemoteObject.cs
91 lines (77 loc) · 2.91 KB
/
BridgeAddRemoteObject.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Copyright (C) Microsoft Corporation. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
using System.Runtime.InteropServices;
using System.Collections.Generic;
using System.Reflection;
using System;
using System.Threading.Tasks;
namespace WebView2WpfBrowser
{
// BridgeAddRemoteObject is a .NET object that implements IDispatch and works with AddRemoteObject.
// See the AddRemoteObjectCmdExecute method that demonstrates how to use it from JavaScript.
#pragma warning disable CS0618
// The.NET version of CoreWebView2.AddHostObjectToScript currently relies on the host object
// implementing IDispatch and so uses the deprecated ClassInterfaceType.AutoDual feature of.NET.
// This may change in the future, please see https://github.com/MicrosoftEdge/WebView2Feedback/issues/517 for more information
[ClassInterface(ClassInterfaceType.AutoDual)]
#pragma warning restore CS0618
[ComVisible(true)]
public class AnotherRemoteObject
{
// Sample property.
public string Prop { get; set; } = "AnotherRemoteObject.Prop";
}
#pragma warning disable CS0618
[ClassInterface(ClassInterfaceType.AutoDual)]
#pragma warning restore CS0618
[ComVisible(true)]
public class BridgeAddRemoteObject
{
// Sample function that takes a parameter.
public string Func(string param)
{
return "BridgeAddRemoteObject.Func(" + param + ")";
}
public async Task<string> FuncAsync(int msDelay)
{
if (msDelay > 0)
{
await Task.Delay(msDelay);
}
return $"BridgeAddRemoteObject.FuncAsync({msDelay})";
}
// Sample function that takes no parameters.
public string Func2()
{
return "BridgeAddRemoteObject.Func2()";
}
public async Task<string> Func2Async()
{
await Task.Delay(500);
return "BridgeAddRemoteObject.Func2Async()";
}
// Get type of an object.
public string GetObjectType(object obj)
{
return obj.GetType().Name;
}
public async Task<string> GetObjectTypeAsync(object obj)
{
await Task.Delay(500);
return GetObjectType(obj);
}
// Sample property.
public string Prop { get; set; } = "BridgeAddRemoteObject.Prop";
public DateTime DateProp { get; set; } = DateTime.UtcNow;
public AnotherRemoteObject AnotherObject { get; set; } = new AnotherRemoteObject();
// Sample indexed property.
[System.Runtime.CompilerServices.IndexerName("Items")]
public string this[int index]
{
get { return m_dictionary[index]; }
set { m_dictionary[index] = value; }
}
private Dictionary<int, string> m_dictionary = new Dictionary<int, string>();
}
}