I wanted to dynamically show and hide a tab in a TabbedPage. When the app starts, all tabs are displayed correctly, but if I remove the tab and then add it again, the content of the tab is not displayed.

This is how it looks on start and after readding

<TabbedPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="MauiTabbed.MainPage"
             BackgroundColor="Yellow">

    <NavigationPage Title="Home">
        <x:Arguments>
            <ContentPage Title="Home" BackgroundColor="Aquamarine">
                <Button
                x:Name="ToogleBtn"
                Text="Toogle Tab" 
                TextColor="Black"
                Clicked="ToggleTab"
                HorizontalOptions="Fill"
                BackgroundColor="White"/>
            </ContentPage>
        </x:Arguments>
    </NavigationPage>

    <NavigationPage Title="Toggled Tab">
        <x:Arguments>
            <ContentPage Title="Toggled Tab">
                <StackLayout Padding="5, 25" BackgroundColor="Red">
                    <Label Text="Toggled Tab"
                           FontAttributes="Bold"
                           FontSize="18"
                           HorizontalOptions="Center" />
                </StackLayout>
            </ContentPage>
        </x:Arguments>
    </NavigationPage>
</TabbedPage>
        Page _savedPage;

        private async void ToggleTab(object sender, EventArgs e)
        {
            if (Children.Count == 1)
            {
                await MainThread.InvokeOnMainThreadAsync(() =>
                {
                    Children.Insert(1, _savedPage);
                });
            }
            else if (Children.Count == 2)
            {
                _savedPage = Children[1];
                await MainThread.InvokeOnMainThreadAsync(() =>
                {
                    Children.RemoveAt(1);
                });
            }
        }

New contributor

user24002065 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Khám phá các thẻ bài đăng