在 blazor 中单击切换按钮时如何使 body 向左移动

how to make body shifted to left when toggle button clicked in blazor

在 blazor 应用程序中,当我单击侧边栏中的切换按钮导航菜单时,它只会折叠侧边栏,而主 body 会保留在那里。我想让侧边栏不可见,并且 main body 向左移动。于是我在mainlayout.layzor中写了如下代码:

@inherits LayoutComponentBase
    @code {
      private bool collapseNavMenu = true;

      private string NavMenuCssClass => collapseNavMenu ? "collapse" : null;

      private void ToggleNavMenu() {
          collapseNavMenu = !collapseNavMenu;
      }
    }
<div class="page">
    <div class="sidebar">
        <div class="pos-f-t">           
            <nav class="navbar navbar-dark bg-blue top-row px-4">
                <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarToggleExternalContent" aria-controls="navbarToggleExternalContent" aria-expanded="false" aria-label="Toggle navigation" @onclick="ToggleNavMenu">
                    <span class="navbar-toggler-icon"></span>
                </button>
            </nav>

            <div class="@NavMenuCssClass">
                <ul class="nav flex-column">
                    <li class="nav-item px-3">
                        <NavLink class="nav-link" href="" Match="NavLinkMatch.All">
                            <span class="oi oi-home" aria-hidden="true"></span> Home
                        </NavLink>
                    </li>
                    <li class="nav-item px-3">
                        <NavLink class="nav-link" href="counter">
                            <span class="oi oi-plus" aria-hidden="true"></span> Counter
                        </NavLink>
                    </li>
                    <li class="nav-item px-3">
                        <NavLink class="nav-link" href="fetchdata">
                            <span class="oi oi-list-rich" aria-hidden="true"></span> Fetch data
                        </NavLink>
                    </li>
                </ul>
            </div>
        </div>
    </div>

    <div class="main">
        <div class="top-row px-4">
            <a href="https://docs.microsoft.com/aspnet/" target="_blank">About</a>
        </div>

        <div class="content px-4">
            @Body
        </div>
    </div>
    </div>

有人可以帮助让 main @Body 在导航栏不可见时向左移动吗?

提前致谢。 俊

我发现我可以在 vanilla blazor 应用程序中管理它们。

我在 mainlayout.lazor 上更改为:

@inherits LayoutComponentBase

<div class="top-row px-4">
<button class="btn-primary" @onclick="ToggleNavMenu">
    <i class="material-icons">view_headline</i>
</button>
</div>
<div class="page">
<div class="sidebar" style="@leftMenuStyle">
    <NavMenu />
</div>

<div class="main">
    <div class="content px-4">
        @Body
    </div>
</div>
</div>

然后我像这样从 MavMenu.razor 中删除了切换按钮。

<div >
<ul class="nav flex-column">
    <li class="nav-item px-3">
        <NavLink class="nav-link" href="" Match="NavLinkMatch.All">
            <i class="material-icons">
                accessibility
            </i> Home
        </NavLink>
    </li>
    <li class="nav-item px-3">
        <NavLink class="nav-link" href="counter">
            <i class="material-icons">autorenew</i>
            Counter
        </NavLink>
    </li>
    <li class="nav-item px-3">
        <NavLink class="nav-link" href="fetchdata">
            <i class="material-icons">autorenew</i> Fetch data
        </NavLink>
    </li>
</ul>
</div>