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

HybridWebView doesn't accept strings returned by JS method invoke #26765

Open
Eilon opened this issue Dec 20, 2024 · 2 comments · May be fixed by #27068
Open

HybridWebView doesn't accept strings returned by JS method invoke #26765

Eilon opened this issue Dec 20, 2024 · 2 comments · May be fixed by #27068
Assignees
Labels
area-controls-hybridwebview HybridWebView control s/triaged Issue has been reviewed t/bug Something isn't working
Milestone

Comments

@Eilon
Copy link
Member

Eilon commented Dec 20, 2024

Description

Have a JS method like this:

		function getRandomHexColor(){
			const randomColor = Math.floor(Math.random() * 16777215).toString(16);
			return '#' + randomColor.padStart(6, '0');
		}

And call it using HybridWebView like this:

var result = await hybridWebView.InvokeJavaScriptAsync("getRandomHexColor", HybridSampleJSContext.Default.String);

And you get an exception saying that .NET can't parse the value (invalid '#' characters).

This should presumably just work, especially because strings are so common. This could necessitate a different API because the current API seems to always expect JSON being returned, so in that sense it 'correctly' fails, but we should find a way to make this likely common scenario work.

Steps to Reproduce

No response

Link to public reproduction project repository

No response

Version with bug

9.0.21 SR2.1

Is this a regression from previous behavior?

No, this is something new

Last version that worked well

No response

Affected platforms

iOS, Android, Windows, macOS

Affected platform versions

No response

Did you find any workaround?

No response

Relevant log output

@mattleibow
Copy link
Member

Why do numbers work then?

This looks like it should just work, so it is strange:

var typedResult = JsonSerializer.Deserialize(stringResult, invokeJavaScriptRequest.ReturnTypeJsonTypeInfo);

I have passed the return type as string, so why is it trying to parse it? I guess it is looking for quotes to remove first?

@mattleibow
Copy link
Member

Well, this may be simpler than expected. If the return type is a string... don't deserialize!

@mattleibow mattleibow moved this from Todo to In Progress in MAUI SDK Ongoing Jan 13, 2025
@mattleibow mattleibow self-assigned this Jan 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-controls-hybridwebview HybridWebView control s/triaged Issue has been reviewed t/bug Something isn't working
Projects
Status: In Progress
Development

Successfully merging a pull request may close this issue.

3 participants