diff --git a/HybridWebView/HybridWebView.cs b/HybridWebView/HybridWebView.cs index 5f7d001..8e1d565 100644 --- a/HybridWebView/HybridWebView.cs +++ b/HybridWebView/HybridWebView.cs @@ -220,7 +220,7 @@ public virtual async Task OnProxyRequestMessage(HybridWebViewProxyEventArgs args var paramObjectValues = invokeData.ParamValues? - .Zip(invokeMethod.GetParameters(), (s, p) => JsonSerializer.Deserialize(s, p.ParameterType)) + .Zip(invokeMethod.GetParameters(), (s, p) => s == null ? null : JsonSerializer.Deserialize(s, p.ParameterType)) .ToArray(); return invokeMethod.Invoke(JSInvokeTarget, paramObjectValues); diff --git a/HybridWebView/KnownStaticFiles/HybridWebView.js b/HybridWebView/KnownStaticFiles/HybridWebView.js index 54e4bfc..806dce0 100644 --- a/HybridWebView/KnownStaticFiles/HybridWebView.js +++ b/HybridWebView/KnownStaticFiles/HybridWebView.js @@ -20,7 +20,10 @@ window.HybridWebView = { paramValues = [paramValues]; } for (var i = 0; i < paramValues.length; i++) { - paramValues[i] = JSON.stringify(paramValues[i]); + // Let 'null' and 'undefined' be passed as-is, but stringify all other values + if (paramValues[i] != null) { + paramValues[i] = JSON.stringify(paramValues[i]); + } } } diff --git a/MauiCSharpInteropWebView/MainPage.xaml.cs b/MauiCSharpInteropWebView/MainPage.xaml.cs index 719e434..9a528dd 100644 --- a/MauiCSharpInteropWebView/MainPage.xaml.cs +++ b/MauiCSharpInteropWebView/MainPage.xaml.cs @@ -175,7 +175,7 @@ private void WriteToLog(string message) private sealed class MyJSInvokeTarget { - private MainPage _mainPage; + private readonly MainPage _mainPage; public MyJSInvokeTarget(MainPage mainPage) { @@ -187,6 +187,16 @@ public void CallMeFromScript(string message, int value) _mainPage.WriteToLog($"I'm a .NET method called from JavaScript with message='{message}' and value={value}"); } + public void CallEmptyParams(string thisIsNull, int thisIsUndefined) + { + _mainPage.WriteToLog($"I'm a .NET method called from JavaScript with null='{thisIsNull}' and undefined={thisIsUndefined}"); + } + + public void CallNoParams() + { + _mainPage.WriteToLog($"I'm a .NET method called from JavaScript with no params"); + } + /// /// An example of a round trip method that takes an input multiple parameters and returns a simple value type (string). /// diff --git a/MauiCSharpInteropWebView/Resources/Raw/hybrid_root/methodinvoke.html b/MauiCSharpInteropWebView/Resources/Raw/hybrid_root/methodinvoke.html index daf249d..8c66779 100644 --- a/MauiCSharpInteropWebView/Resources/Raw/hybrid_root/methodinvoke.html +++ b/MauiCSharpInteropWebView/Resources/Raw/hybrid_root/methodinvoke.html @@ -11,15 +11,21 @@ return sum; } - /* Async invoke examples */ - - function CallDotNetMethod() { Log('Calling a method in .NET with some parameters'); HybridWebView.SendInvokeMessageToDotNet("CallMeFromScript", ["msg from js", 987]); } + function CallEmptyParams() { + HybridWebView.SendInvokeMessageToDotNet("CallEmptyParams", [null, undefined]); + } + + function CallNoParams() { + HybridWebView.SendInvokeMessageToDotNet("CallNoParams"); + } + + /* Async invoke examples */ async function invokeRoundTripDotNetMethod() { var r = await HybridWebView.SendInvokeMessageToDotNetAsync('RoundTripCallFromScript', ["param1", 123]); Log(r); @@ -64,6 +70,8 @@

HybridWebView demo: Method invoke

Sync invoke - Call .NET, no response
+ +