Mudblazor:从 ChildComponents 更改 AppBarText

问题描述 投票:0回答:1

如何更改子组件中的 MudAppBar 文本? 我有一个子组件(EditCustomer)。我想从 EditCustomer 更改 appbartext。

主布局。剃刀:

    <MudLayout>
        <MudAppBar Elevation="1">
            <MudIconButton Icon="@Icons.Material.Filled.Menu" Color="Color.Inherit" Edge="Edge.Start" OnClick="@((e) => DrawerToggle())" />
            @appBarText
            <MudSpacer />
            <MudIconButton Icon="@Icons.Material.Filled.MoreVert" Color="Color.Inherit" Edge="Edge.End" />
        </MudAppBar>
        <MudDrawer @bind-Open="_drawerOpen" Elevation="2">
            <MudDrawerHeader>
                <MudText Typo="Typo.h5" Class="mt-1">Application</MudText>
            </MudDrawerHeader>
            <NavMenu></NavMenu>
        </MudDrawer>
        <MudMainContent>
            @Body
        </MudMainContent>
    </MudLayout>

@code {
    bool _drawerOpen = true;
    private string appBarText = "Default AppBar Text";

    void DrawerToggle()
    {
        _drawerOpen = !_drawerOpen;
    }
}
navmenu.razor:

<MudNavMenu>
    <MudNavLink Href="/" Match="NavLinkMatch.All">Dashboard</MudNavLink>
    <MudNavLink Href="/Home" Match="NavLinkMatch.Prefix">Home</MudNavLink>
    <MudNavGroup Title="TESTING" Expanded="true">
        <MudNavLink Href="/test1" Match="NavLinkMatch.Prefix">test1</MudNavLink>
        <MudNavLink Href="/test2" Match="NavLinkMatch.Prefix">test2</MudNavLink>
    </MudNavGroup>
    <MudNavLink Href="/about" Match="NavLinkMatch.Prefix">About</MudNavLink>
</MudNavMenu>
blazor mudblazor
1个回答
0
投票

一个简单的解决方案是直接在子组件中使用布局方法。如:

MainLayout.razor

...
        <CascadingValue Value="this">
                    @Body
        </CascadingValue>
...
@code{
    private string appBarText = "Default AppBar Text";
    public void ChangeText(string message)
    {
        appBarText = message;
        StateHasChanged();
    }
...
}

Home.剃须刀

@page "/"
@using MudBlazorWebApp3.Components.Layout


<button @onclick="UseLayoutMethod">Change text</button>
@code{
    [CascadingParameter]

    private MainLayout MainLayout { get; set; }

    private async Task UseLayoutMethod()
    {
        MainLayout.ChangeText("Hello from Child Component!");
    }
}
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.