dotnet / MobileBlazorBindings

Experimental Mobile Blazor Bindings - Build native and hybrid mobile apps with Blazor
MIT License
1.2k stars 168 forks source link

BlazorWebView interaction #417

Open ghost opened 2 years ago

ghost commented 2 years ago

This is not completely related to BlazorMobile bindings, But I created a windows forms app and then render a razor component inside BlazorWebView control:

`BlazorWebView blazorApp = new BlazorWebView()
 {
     Dock = DockStyle.Fill,
     HostPage = "wwwroot/index.html",
     Services = serviceProvider 
 };
 blazorApp.RootComponents.Add<App>("#app");

 var form1 = new Form1();
 form1.Controls.Add(blazorApp);
 Application.Run(form1);`

To create a good hybrid app that uses both blazor and win forms, I need to interact between blazor component inside BlazorWebView and Form1, So that I can send data to or call component public methods from Form1 and recieve notifications from the component back to Form1, But I can't find a suitable member in the BlazorWebView's list of members to do this. Is there any way to do this?

Eilon commented 2 years ago

@mg1357 , in general the way to do this is so define a "service" in the Dependency Injection (DI) container and then use that service from both the Blazor UI using the @inject directive, and also from the native UI (WinForms/WPF/etc.).

You can learn more about DI here: https://docs.microsoft.com/mobile-blazor-bindings/advanced/dependency-injection