Search code examples
maui-blazorrazor-class-library

MAUI-Blazor add AdditionalAssembly programmatically


I am trying to found a good way to arrange my Razor class libraries to let use them in MAUI Blazor hybrid applications

My problem is about the route when the class libraries contains componentes with @page directive. I saw that if I have a MAUI Blazor application and I want to use a Razor class library (that contains pages) I have to add a reference to the library and add to in Main.razor at Router AdditionalAssemblies the asembly of Razor class library. I not like so much this, I am used that if I add a referece to a class library, all the library "resources" are availables, anyway is accettable.

My big problem is when my library use another library that also contains pages. In this case I have to tell to my library user the list of assembly that my library uses and he have to add in the him Router component AdditionalAssemblies and more are the libraries my main library uses more long is the list of assebly I have to provide to my library users to let them add to their AdditionalAssemblies application Roter component. This for me is very ugly so I am looking for a way to avoid this, for example I'd like tell to the library users "add this UseMyLibrary() in the service configuration(or somethig like this)" and this method will do all the work that the router component do for the AdditionalAssemblies.

Is this possible?


Solution

  • I decided to do like that, aeach library exposes a class like this:

    public static class RouteAdditionalAssemblies
    {
        public static IEnumerable<Assembly> AdditionalAssemblies
        {
            get
            {
                return Enumerable.Repeat(typeof(RouteAdditionalAssemblies).Assembly, 1).Concat(Library2.RouteAdditionalAssemblies.AdditionalAssemblies);
            }
        }
    }
    

    So in the Main.razor at Router AdditionalAssemblies I can just Add

    <Router AppAssembly="@typeof(Main).Assembly"
            AdditionalAssemblies="Library1.RouteAdditionalAssemblies.AdditionalAssemblies ">
    

    So, if library1 will add some reference to another library that contains pages, I have to add it just on Library1.RouteAdditionalAssemblies.AdditionalAssemblies property