Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix passing null/undefined params to method invoke #54

Merged
merged 1 commit into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading