MobileBlazorBindings icon indicating copy to clipboard operation
MobileBlazorBindings copied to clipboard

Reusable blazor components

Open angelru opened this issue 4 years ago • 10 comments
trafficstars

I have a web application in blazor wasm, I would like to reuse blazor wasm login and registration in my xamarin application, is there a way to use my 2 blazor components in xamarin?

my xamarin app is not blazor bindings.

angelru avatar Jun 14 '21 09:06 angelru

@angelru yes, in general you should be able to add a BlazorWebView control from Mobile Blazor Bindings to your Xamarin.Forms app, and you can then host your Blazor web components in that BlazorWebView.

Eilon avatar Jun 15 '21 16:06 Eilon

@Eilon I have several doubts, my xamarin forms project is not from Mobile Blazor Bindings, I would have to install the nuget package and then the configurations no? then what I do not see clear is to put only my 2 components that I want to reuse, since I do not want the complete application.

angelru avatar Jun 16 '21 06:06 angelru

Hi @angelru it's probably simpler than you might think 😄 A Mobile Blazor Bindings app is really a Xamarin.Forms app that happens to use a BlazorWebView control that loads some Blazor. There are a few extra project settings in a few places. We have minimal docs on this topic right now, but in general, the process is to create a new empty Blazor Hybrid app, look at the various settings and contents of the project, and copy it to your existing Xamarin.Forms app.

Eilon avatar Jun 16 '21 15:06 Eilon

@Eilon Thank you very much, I'll see what I can do ...

angelru avatar Jun 18 '21 10:06 angelru

so it's basically adding a browser in your xamarin app? My blazor component can't communicate with the others c# codes in my xamarin because they are running in a sandox inside a browser?

Xyncgas avatar Jun 23 '21 10:06 Xyncgas

@Xyncgas that isn't the case. All the C# code in a Mobile Blazor Bindings app runs in the same process and can all "talk" to each other. There's no browser sandbox for the C# code, and there's no Web Assembly. The browser is only used to render the HTML, and optionally run any custom JS that you might have (e.g. 3rd party component such a Bootstrap, or your own custom JS code).

Eilon avatar Jun 23 '21 16:06 Eilon

@Xyncgas that isn't the case. All the C# code in a Mobile Blazor Bindings app runs in the same process and can all "talk" to each other. There's no browser sandbox for the C# code, and there's no Web Assembly. The browser is only used to render the HTML, and optionally run any custom JS that you might have (e.g. 3rd party component such a Bootstrap, or your own custom JS code).

Ohh, so people found a way to let the MBB App talk to the browser and tell it what to render, while MBBA itself not running in the browser? that's so cool !!! what kind of magic is this

Xyncgas avatar Jun 24 '21 08:06 Xyncgas

@Xyncgas both Mobile Blazor Bindings (experimental) and Blazor Desktop (supported) use the same technique to run the app. All the .NET code runs in the same process, it's just that the Blazor code renders the HTML into a WebView control for the UI to show up. No C#/.NET code runs "in" the WebView control (so there's no Web Assembly here, and also no SignalR). I talk about this a bit on this video I recently made: https://www.youtube.com/watch?v=oNxUfPoAXac

Eilon avatar Jul 09 '21 17:07 Eilon

Is there a new way or is what you told me in this thread the correct way? or do we have to wait for MAUI?

angelru avatar Aug 19 '21 11:08 angelru

@angelru the way this works is the same between Mobile Blazor Bindings and .NET MAUI w/ BlazorWebView. You can place your various Blazor components in a Razor Class Library (RCL) and then reference them from Blazor WebAssembly, Blazor Server, or Blazor Desktop (in .NET MAUI, WinForms, or WPF).

Eilon avatar Aug 19 '21 16:08 Eilon