1111
1212namespace Files . App . Extensions
1313{
14+ using WebViewMessageReceivedHandler = TypedEventHandler < WebView2 , CoreWebView2WebMessageReceivedEventArgs > ;
15+
1416 /// <summary>
1517 /// Code modified from https://gist.github.com/mqudsi/ceb4ecee76eb4c32238a438664783480
1618 /// </summary>
@@ -47,15 +49,12 @@ private struct PropertyWebMessage
4749 public string Value { get ; set ; }
4850 }
4951
50- public static List < TypedEventHandler < WebView2 , CoreWebView2WebMessageReceivedEventArgs > > _handlers = new List < TypedEventHandler < WebView2 , CoreWebView2WebMessageReceivedEventArgs > > ( ) ;
52+ public static List < WebViewMessageReceivedHandler > _handlers = new ( ) ;
5153 public static async Task AddWebAllowedObject < T > ( this WebView2 webview , string name , T @object )
5254 {
5355 var sb = new StringBuilder ( ) ;
5456 sb . AppendLine ( $ "window.{ name } = {{ ") ;
5557
56- // Test webview for our sanity
57- await webview . ExecuteScriptAsync ( $@ "console.log(""Sanity check from iMessage"");") ;
58-
5958 var methodsGuid = Guid . NewGuid ( ) ;
6059 var methodInfo = typeof ( T ) . GetMethods ( BindingFlags . Public | BindingFlags . Instance ) ;
6160 var methods = new Dictionary < string , MethodInfo > ( methodInfo . Length ) ;
@@ -79,7 +78,6 @@ public static async Task AddWebAllowedObject<T>(this WebView2 webview, string na
7978 // if (property.CanWrite)
8079 // {
8180 // sb.AppendLine($@"set {propertyName}(value) {{ window.chrome.webview.postMessage(JSON.stringify({{ guid: ""{propertiesGuid}"", id: this._callbackIndex++, property: ""{propertyName}"", action: ""{(int)PropertyAction.Write}"", value: JSON.stringify(value) }})); const promise = new Promise((accept, reject) => this._callbacks.set(this._callbackIndex, {{ accept: accept, reject: reject }})); return promise; }},");
82-
8381 // }
8482 // properties[propertyName] = property;
8583 //}
@@ -101,7 +99,7 @@ public static async Task AddWebAllowedObject<T>(this WebView2 webview, string na
10199 // So we can see it in the JS debugger
102100 }
103101
104- var handler = ( TypedEventHandler < WebView2 , CoreWebView2WebMessageReceivedEventArgs > ) ( async ( _ , e ) =>
102+ var handler = ( WebViewMessageReceivedHandler ) ( async ( _ , e ) =>
105103 {
106104 var message = JsonConvert . DeserializeObject < WebMessage > ( e . TryGetWebMessageAsString ( ) ) ;
107105 if ( message . Guid == methodsGuid )
@@ -187,7 +185,7 @@ public static async Task AddWebAllowedObject<T>(this WebView2 webview, string na
187185 }
188186 catch ( Exception ex )
189187 {
190- var json = JsonConvert . SerializeObject ( ex , new JsonSerializerSettings ( ) { Error = ( _ , e ) => e . ErrorContext . Handled = true } ) ;
188+ // var json = JsonConvert.SerializeObject(ex, new JsonSerializerSettings() { Error = (_, e) => e.ErrorContext.Handled = true });
191189 //await webview.ExecuteScriptAsync($@"{name}._callbacks.get({propertyMessage.Id}).reject(JSON.parse({json})); {name}._callbacks.delete({propertyMessage.Id});");
192190 //throw;
193191 }
0 commit comments