Skip to content

MAUI Project can not import web-components script automatically #404

Closed

Description

@RiuHDuo

I created a MAUI-Blazor project with VS 2022.
and installed Microsoft.Fast.Components.FluentUI 2.3.2
image

Add @using Microsoft.Fast.Components.FluentUI in _Imports.razor.
image

Add tag in index.html
image

Followed documents modified Promgram.cs
image

Add sample code in index.razor

<FluentCard Style="padding: 1.5rem; width: 400px; height: 250px; ">
  <h2>Hello World!</h2>
  <FluentButton Appearance="@Appearance.Accent">Click Me</FluentButton>
</FluentCard>

App can not work well.
image

If add web-component script in index.html.
image

it will work well.
image

Activity

  1. changed the title [-]MAUI Project can not import web-components script.automatically[/-] [+]MAUI Project can not import web-components script automatically[/+] on May 23, 2023
  2. LuohuaRain commented on May 31, 2023

    @LuohuaRain
    Contributor

    In v2.3.4, this issue can still be reproduced. And same with (WPF) Blazor app

    public static class MauiProgram
    {
        public static MauiApp CreateMauiApp()
        {
            var builder = MauiApp.CreateBuilder();
            builder
                .UseMauiApp<App>()
                .ConfigureFonts(fonts =>
                {
                    fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
                });
    
            builder.Services.AddMauiBlazorWebView();
    
            builder.Services.AddFluentUIComponents(options =>
            {
                options.HostingModel = BlazorHostingModel.Hybrid;
                options.IconConfiguration = new(true)
                {
                    Sizes = new[] { IconSize.Size20, IconSize.Size24, IconSize.Size32 },
                    Variants = new[] { IconVariant.Filled, IconVariant.Regular }
                };
                options.EmojiConfiguration = new(true)
                {
                    Groups = new[] { EmojiGroup.Activities, EmojiGroup.Flags },
                    Styles = new[] { EmojiStyle.Color }
                };
            });
    
            builder.Services.AddScoped<IStaticAssetService, FileBasedStaticAssetService>();
    
    #if DEBUG
            builder.Services.AddBlazorWebViewDeveloperTools();
            builder.Logging.AddDebug();
    #endif
    
    
            return builder.Build();
        }
    }
  3. andreisaperski commented on May 31, 2023

    @andreisaperski
    Contributor

    Looks like the root cause for this issue is the same as for #327.

    Microsoft.Fast.Components.FluentUI package has an initializer Microsoft.Fast.Components.FluentUI.lib.module.js which loads web-components.js, but this initializer is ignored by Blazor Hybrid.

    Build generates [AppName].modules.json file (bin[Configuration][FW][Platform]\AppX\wwwroot[AppName].modules.json) and populates it with found initilizers, but blazor.webview.js doesn't use this file.
    It loads initializers from '_framework/blazor.modules.json' instead. blazor.modules.json contains an empty array, it's embeded as resource in Microsoft.AspNetCore.Components.WebView and provided to blazor.webview.js by WebViewManager as is - array of initializers is always empty.

    As sort of PoC, replaced '_framework/blazor.modules.json' with 'MauiApp1.modules.json', rebuilt Microsoft.AspNetCore.Components.WebView and copied new version into bin folder:

    image

    Microsoft.Fast.Components.FluentUI.lib.module.js was loaded and controls initialized properly

  4. vnbaaij commented on May 31, 2023

    @vnbaaij
    Collaborator

    Andrei Saperski (@andreisaperski) Can you test if custom web component events (like accordion change event) work with this setup as well?

  5. andreisaperski commented on May 31, 2023

    @andreisaperski
    Contributor

    Copied content of AccordionDefault.razor into Index.razor of MAUI App, initial state:

    image

    After click on 'Panel two':
    image

    Handler for @onaccordionchange was called and Accordion raised OnAccordionItemChange

  6. vnbaaij commented on May 31, 2023

    @vnbaaij
    Collaborator

    So if people want to use the library in MAUI, we just need to tell them to compile their own WebView and everything works! 🤣🤣

  7. andreisaperski commented on May 31, 2023

    @andreisaperski
    Contributor

    There's another option )
    image

  8. vnbaaij commented on Jun 2, 2023

    @vnbaaij
    Collaborator

    Andrei Saperski (@andreisaperski) please chime in on the issue in the aspnetcore repo I linked to above

  9. andreisaperski commented on Jun 2, 2023

    @andreisaperski
    Contributor

    Vincent Baaij (@vnbaaij) not sure if i can add anything there, the comment you linked there, reflects all i have

  10. andreisaperski commented on Jun 8, 2023

    @andreisaperski
    Contributor

    There's a simpler workaround: intercept '_framework/blazor.modules.json' and provide proper JS initializers file (created by build). You can drop this initializersLoader.windows.js into wwwroot folder of app and add script tag for it to index.html right before '_framework/blazor.webview.js' tag:

    <script app-name="FluentUI.Demo.Hybrid.MAUI" src="./_content/FluentUI.Demo.Hybrid.Shared/js/initializersLoader.windows.js"></script>
    <script src="_framework/blazor.webview.js"></script>

    'app-name' attribute needs to match app's assembly name - initializersLoader uses 'app-name' to resolve name of the file with initializers.
    initializersLoader replaces 'fetch' function with another one which provides the right file instead of empty blazor.modules.json. 'fetch' is restored to its original state once '_framework/blazor.modules.json' request is intercepted.

    When original issue is resolved, all that need to be done is to remove the tag and the script.

  11. vnbaaij commented on Jun 9, 2023

    @vnbaaij
    Collaborator

    Adding these instructions to the Blazor Hybrid section in the readme!!

  12. vnbaaij commented on Jun 10, 2023

    @vnbaaij
    Collaborator
  13. Kalyxt commented on Sep 11, 2024

    @Kalyxt

    There's a simpler workaround: intercept '_framework/blazor.modules.json' and provide proper JS initializers file (created by build). You can drop this initializersLoader.windows.js into wwwroot folder of app and add script tag for it to index.html right before '_framework/blazor.webview.js' tag:

    <script app-name="FluentUI.Demo.Hybrid.MAUI" src="./_content/FluentUI.Demo.Hybrid.Shared/js/initializersLoader.windows.js"></script>
    <script src="_framework/blazor.webview.js"></script>
    

    'app-name' attribute needs to match app's assembly name - initializersLoader uses 'app-name' to resolve name of the file with initializers. initializersLoader replaces 'fetch' function with another one which provides the right file instead of empty blazor.modules.json. 'fetch' is restored to its original state once '_framework/blazor.modules.json' request is intercepted.

    When original issue is resolved, all that need to be done is to remove the tag and the script.

    Doesn't work for me. I am running it on android emulator and all I get is [chromium] [INFO:CONSOLE(1)] "Uncaught (in promise) SyntaxError: Unexpected token ?", source: https://0.0.0.0/_content/Microsoft.FluentUI.AspNetCore.Components/Microsoft.FluentUI.AspNetCore.Components.lib.module.js (1) . App is then frozen at startup. I am using one of Geralds projects in MAUI blazor hybrid workshop.

    edgeinspector
    monkeyfinder

  14. vnbaaij commented on Sep 11, 2024

    @vnbaaij
    Collaborator

    Kalyxt is this on .NET 8 or 9?

  15. Kalyxt commented on Sep 12, 2024

    @Kalyxt

    Vincent Baaij (@vnbaaij) .NET 8

    <PackageReference Include="Microsoft.FluentUI.AspNetCore.Components" Version="4.9.3" />
    <PackageReference Include="Microsoft.Maui.Controls" Version="8.0.82" />
    <PackageReference Include="Microsoft.Maui.Controls.Compatibility" Version="8.0.82" />
    <PackageReference Include="Microsoft.AspNetCore.Components.WebView.Maui" Version="8.0.82" />

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions