Skip to content

Commit

Permalink
Fix passing null/undefined params to method invoke (#54)
Browse files Browse the repository at this point in the history
Fixes #48
  • Loading branch information
Eilon committed Mar 12, 2024
1 parent aa09320 commit 3ca8010
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 6 deletions.
2 changes: 1 addition & 1 deletion HybridWebView/HybridWebView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 4 additions & 1 deletion HybridWebView/KnownStaticFiles/HybridWebView.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
}
}

Expand Down
12 changes: 11 additions & 1 deletion MauiCSharpInteropWebView/MainPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ private void WriteToLog(string message)

private sealed class MyJSInvokeTarget
{
private MainPage _mainPage;
private readonly MainPage _mainPage;

public MyJSInvokeTarget(MainPage mainPage)
{
Expand All @@ -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");
}

/// <summary>
/// An example of a round trip method that takes an input multiple parameters and returns a simple value type (string).
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -64,6 +70,8 @@ <h1>HybridWebView demo: Method invoke</h1>
<b>Sync invoke - Call .NET, no response</b>
<br />
<button type="button" onclick="CallDotNetMethod()">Call .NET method with some parameters</button>
<button type="button" onclick="CallEmptyParams()">Call .NET method with null/undefined parameters</button>
<button type="button" onclick="CallNoParams()">Call .NET method with no parameters</button>
</p>

<p>
Expand Down

0 comments on commit 3ca8010

Please sign in to comment.