我正在使用.net8和Blazor和流畅的UI库,我得到一个错误,即使我在MainLayout的末尾将其添加到App.razor App.razor中,也没有找到任何提供者.

Program.cs

builder.Services.AddHttpClient();
builder.Services.AddFluentUIComponents();

App.razor App.razor

<script src="_content/Microsoft.FluentUI.AspNetCore.Components/js/loading-theme.js" type="text/javascript"></script>
<script src="_content/Microsoft.FluentUI.AspNetCore.Components/Microsoft.FluentUI.AspNetCore.Components.lib.module.js" type="module" async></script>

MainLayout.razor

<FluentLayout>
    <FluentHeader>
        <Header/>
    </FluentHeader>

    <FluentStack Orientation="Orientation.Horizontal" Width="100%" Class="page--container">
        <NavMenu/>

        <FluentBodyContent>
            @Body
        </FluentBodyContent>
    </FluentStack>
</FluentLayout>

<FluentDialogProvider/>
<FluentToastProvider/>
<FluentTooltipProvider/>

Header.razor

[Inject] public IDialogService DialogService { get; set; }

private async Task ToggleThemeHandler()
{
    var dialog = DialogService.ShowDialogAsync<MenuPanel>(new DialogParameters
    {
        Alignment = HorizontalAlignment.Right
    });
    await dialog;
}

MenuPanel.razor

@implements IDialogContentComponent

<FluentDialogBody>
    Menu
</FluentDialogBody>

IDE控制台

Unhandled exception rendering component: <FluentDialogProvider /> needs to be added to the main layout of your application/site. (Parameter 
'OnShowAsync')

System.ArgumentNullException: <FluentDialogProvider /> needs to be added to the main layout of your application/site. (Parameter 'OnShowAsyn
c') at Microsoft.FluentUI.AspNetCore.Components.DialogService.ShowDialogAsync[TData](Type dialogComponent, TData data, DialogParameters param
eters) in /_/src/Core/Components/Dialog/Services/DialogService-Dialog.cs:line 17 at Microsoft.FluentUI.AspNetCore.Components.DialogService.ShowDialogAsync[TDialog](DialogParameters parameters) in /_/src/Core/Components/Dialog/Services/DialogService-Dialog.cs:line 35 at Components.Layouts.MainLayout.Components.Header.ToggleThemeHandler() in **\Components\Layouts\MainLayout\Components\Header.razor:line 36

推荐答案

我会说,这与另一个模式库中描述的问题完全相同: https://github.com/Blazored/Modal/issues/543

你正在跨越互动的界限.如果你的页面上有@rendermode个,界限是这样的:

<FluentLayout>
    <FluentHeader>
        <Header/>
    </FluentHeader>

    <FluentStack Orientation="Orientation.Horizontal" Width="100%" Class="page--container">
        <NavMenu/>

        <FluentBodyContent>
            @* Interactivity Boundary *@
            @Body
            @* Interactivity Boundary *@
        </FluentBodyContent>
    </FluentStack>
</FluentLayout>

这意味着<Header<FluentLayout<FluentHeader<FluentStack等是服务器端呈现的(SSR),并且页面本身(@Body)具有互动性,因此不同于SSR的上下文.他们彼此之间没有交流.

您必须统一对话框和页面之间的互动性.

在这里,您必须将所有对话框元素放在交互页面中,或者将其放入MainLayout.razor:

@rendermode InteractiveServer

这基本上会在默认情况下使您的所有页面都成为Interactive Server.(这可能是您不想要的,其他解决方案也存在).

Csharp相关问答推荐

为什么在GuardationRule的收件箱函数中,decode.TryParse(valueString,out valueParsed)在给出1.0.1时返回true?

如何使用Unity和MRTK3将手网添加到Hololens 2应用程序中

MongoDB将JS查询转换为C#的问题

应该使用哪一个?"_counter += 1 OR互锁增量(ref_counter)"""

将列表字符串映射为逗号分隔字符串<>

如何告诉自己创建的NuGet包在应用程序中发生了变化?

始终保留数组中的最后N个值,丢弃最老的

如何从HttpContext获取请求正文

. NET JSON属性自定义所需逻辑

更新产品但丢失产品ASP.NET Core的形象

尽管保证密钥不同,但已添加相同密钥的项(&Q;)

Automapper 12.x将GUID映射到字符串

将字节转换为 struct 并返回

当试图限制EF Select 的列时,如何避免重复代码?

Blazor Fluent UI DialogService,<;FluentDialogProvider/>;错误

如何避免在.NET中将日志(log)写入相对路径

如何从非异步任务中正确返回TypeResult

C#中类库项目的源代码生成器

如何在绑定到数据库的datagridview中向上或向下移动行

除非首先访问使用的终结点,否则本地API上的终结点不起作用