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

[Enhancement] HybridWebView.SetInvokeJavaScriptTarget() Multi-target support OR Generator to produce target #27990

Open
Sane7222 opened this issue Feb 23, 2025 · 0 comments

Comments

@Sane7222
Copy link

Hello!

First off, I love what the HybridWebView has introduced into MAUI. However, an immediate issue I encountered is the SetInvokeJavaScriptTarget() method only supports 1 object. If I have many C# classes with methods I want to call from JavaScript, then I would have to write out some duplicate class to pass in.

Related issues: #9424, #27258, #27382

A potential solution is to make a custom attribute

[AttributeUsage(AttributeTargets.Method)]
public class JsTargetAttribute : Attribute { }

Mark methods across classes and files

public interface IFormFactor {
    [JsTarget]
    public string GetFormFactor();
}

public interface IPlatform {
    [JsTarget]
    public string GetPlatform();
}

Have a generator make a source file to pass into SetInvokeJavaScriptTarget()

[Generator]
public class JsTargetGenerator : IIncrementalGenerator {
    public void Initialize(IncrementalGeneratorInitializationContext context) {
        var methods = context.SyntaxProvider.ForAttributeWithMetadataName("JsTargetAttribute");

        var source = """
        public class InvokeJsTarget {
            public string GetFormFactor() => IFormFactor.GetFormFactor();
            public string GetPlatform() => IPlatform.GetPlatform();
        }
        """;

        context.AddSource(InvokeJsTarget.g.cs, source);
    }
}

Would love to hear everyone's thoughts 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant