user20081580
user20081580

Reputation:

MAUI Blazor hybrid project use also for web-project?

Is there a way to somehow use the views created in MAUI Blazor hybrid for a web project as well? Has anyone tried this and are there examples somewhere (I couldn't find anything so far)?

It's depressing anyway that Microsoft is always behind with ideas even now with cross-platforms. Flutter provides not only the common code base for mobile and desktop but also for web!

Thanks

Upvotes: 1

Views: 1533

Answers (3)

user20081580
user20081580

Reputation:

...it's been a while, but there are still perhaps more simple solutions.

On this page you can find a suggested solution:

https://www.telerik.com/blogs/sharing-code-blazor-dotnet-maui

It didn't work for me because I use Code Behind to create my projects so I have a clear separation of views and logic.

I have then also, like in the example above, created an empty project and in it I have then but only two individual projects. Once MAUI native and once Blazor Server.

I shared: Views (Forms), Code-Behind (ViewModels), static helper classes and Models. For code-behind there was still a problem which was described here:

Reusable razor page and code behind class

pcsasa

Upvotes: 1

gonzalorf
gonzalorf

Reputation: 311

I do that by seting up a solution with the following projects:

-Maui Blazor project -Razor Class Library project (RCL, referenced by the other projects) -Blazor WASM project

All pages are in the RCL and can be used by the Maui and the Web projects. The most important thing to keep in mind is that you have to modify the default Main.razor in the Maui project and add the attribute AdditionalAssemblies to add the RCL.

And the paths in the index.html (in both projects, Maui and WASM) need the prefix _content/RCL_ASSEMBLY_NAME/

Upvotes: 1

Gerald Versluis
Gerald Versluis

Reputation: 34013

Officially we don't have anything for that yet. I guess you could do it with some workarounds, but I'm not sure if you should want that.

There is this template from bit platform that provides you with a template that has an app, web, API project and more. I think that shares code between the two as well.

On a side-note:

It's depressing anyway that Microsoft is always behind with ideas even now with cross-platforms.

Maybe it's a conscious choice? To be honest, and this is my personal opinion, I don't see myself ever sharing UI code between web, mobile and desktop. Even mobile and desktop is already a stretch for me, but web I think needs a special treatment altogether to not just be a stretched out version of a mobile app.

In my opinion it will still take a lot of effort to get the UI just right to make it feel like it belongs on that platform and I'm not sure if the pros outweigh the cons.

I see a lot more value in building reusable Razor components that you can share between web and .NET MAUI Blazor/Blazor Hybrid. The duplicate code should be at a minimum with that.

Upvotes: 2

Related Questions