You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

自定义FooterTemplate后,如何关闭Blazor FluentProfileMenu?

Blazor 8 + FluentUI Profile Menu:点击页脚链接跳转后弹窗未关闭的解决方法

我在Blazor 8项目中使用FluentUI的Profile Menu组件时,为给对话框页脚添加图标自定义了FooterTemplate,代码如下:

<FooterTemplate>
    <FluentStack VerticalAlignment="@VerticalAlignment.Center">
        <FluentSpacer />
        <FluentAnchor part="footer-link" Appearance="@Appearance.Hypertext" Href="#" OnClick="@ShowProfile">
            <FluentStack VerticalAlignment="@VerticalAlignment.Center">
                Settings
                <FluentIcon Value="@(new Icons.Regular.Size20.Settings())" /> 
            </FluentStack>
        </FluentAnchor>
    </FluentStack>
</FooterTemplate>

页面显示正常,但点击设置链接跳转页面时,Profile Menu的弹窗始终保持打开状态。推测原因是事件冒泡问题:Profile Menu外层div绑定了@onclick="@(e => PopoverVisible = !PopoverVisible)"事件,而FluentAnchor的点击事件可能阻止了冒泡,导致弹窗无法关闭。

尝试过以下三种方法均无效:

  • 设置FluentAnchor的Href为目标页面并移除OnClick
  • 绑定FluentAnchor的OnClick事件
  • 绑定FluentProfileMenu的OnFooterLinkClick事件
    以上方法都能实现页面跳转,但弹窗始终无法关闭。

可行解决方法(操作繁琐但有效)

  1. 给FluentProfileMenu添加组件引用:
<FluentProfileMenu
    @ref="_fluentProfileMenu"
    <!-- 保留原有其他属性 -->
>
  1. 在代码文件中定义对应的私有属性:
private FluentProfileMenu? _fluentProfileMenu;
  1. 在点击处理方法中,先通过反射关闭弹窗,再执行页面跳转:
public void ManageAccount()
{
    var propertyInfo = _fluentProfileMenu?
        .GetType()
        .GetProperty("Open", 
            BindingFlags.Instance | BindingFlags.Public);
    propertyInfo?.SetValue(_fluentProfileMenu, false);

    NavigationManager.NavigateTo("/Account/Manage");
}

注:直接设置_fluentProfileMenu.Open = false会触发BL005警告,由于项目将警告视为错误无法编译,因此采用反射方式绕过该限制。

内容的提问来源于stack exchange,提问作者Morgan Skinner

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 13:31:12