-
Notifications
You must be signed in to change notification settings - Fork 10
/
WebViewPage.xaml.cs
140 lines (123 loc) · 4.1 KB
/
WebViewPage.xaml.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
using Autodesk.Revit.UI;
using Microsoft.Web.WebView2.Core;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Threading;
namespace RevitWebView2Demo
{
public partial class WebViewPage : Page
{
public static UIDocument uidoc;
public WebViewPage()
{
this.InitializeComponent();
App.SelectEvent.Subscribers.Add(Wv2SelectionChanged);
StartSelectionRaiseTimer();
}
internal class WvReceiveAction
{
public string action;
public object payload;
}
internal class SelectionResult : WvReceiveAction
{
public List<string> payload;
}
public class PostMessage
{
public PostMessage(Wv2SendAction actionName, object actionPayload)
{
action = Enum.GetName(typeof(Wv2SendAction), actionName);
payload = actionPayload;
}
public string action { get; private set; }
public object payload { get; private set; }
}
public enum Wv2SendAction
{
SelectionChanged
}
private void OnWebViewInteraction(object sender, CoreWebView2WebMessageReceivedEventArgs e)
{
WvReceiveAction result = null;
try
{
result = JsonConvert.DeserializeObject<WvReceiveAction>(e.WebMessageAsJson);
}
catch (Exception exception)
{
Debug.WriteLine(exception);
}
if (result == null)
{
return;
}
switch (result.action)
{
case "select":
WebView2EventHandlers.HandleSelect(uidoc, e.WebMessageAsJson);
break;
case "create-sheet":
App.RevitWv2Event.Raise(RevitWv2EventHandler.RevitWv2ActionsEnum.CreateSheet);
break;
default:
Debug.WriteLine("action not defined");
break;
}
}
public async void SendMessage(PostMessage message)
{
try
{
await Dispatcher.InvokeAsync(
() => webView
.ExecuteScriptAsync(
$"dispatchWebViewEvent({JsonConvert.SerializeObject(message)})"
)
);
}
catch (Exception e)
{
Debug.WriteLine(e);
}
}
public void Wv2SelectionChanged(List<string> elids)
{
var elements = elids.Select(x =>
{
var el = uidoc.Document.GetElement(x);
return new
{
name = el.Name,
id = x
};
});
var postMessage = new PostMessage(Wv2SendAction.SelectionChanged, elements);
SendMessage(postMessage);
}
private DispatcherTimer _selectionCheckTimer = null;
private void StartSelectionRaiseTimer()
{
_selectionCheckTimer = new DispatcherTimer();
_selectionCheckTimer.Tick = (s, e) => Dispatcher.Invoke(() => App.SelectEvent.Raise());
_selectionCheckTimer.Interval = TimeSpan.FromMilliseconds(750);
_selectionCheckTimer.Start();
}
private void StopSelectionRaiseTimer()
{
_selectionCheckTimer.Stop();
_selectionCheckTimer = null;
}
private void WebView_OnUnloaded(object sender, RoutedEventArgs e)
{
App.SelectEvent.Subscribers.Remove(Wv2SelectionChanged);
StopSelectionRaiseTimer();
}
}
}