adospace / reactorui-maui

MauiReactor is a MVU UI framework built on top of .NET MAUI
MIT License
555 stars 46 forks source link

TabbedPage shows same ToolbarItem twice #190

Closed Code-DJ closed 6 months ago

Code-DJ commented 6 months ago

ToolbarItem on TabbedPage gets added twice. At first I thought it was adding one for each page but even if I comment new SecondPage() below, it still shows twice.

Note: this is not a new issue, it's been there for a while. My workaround has been to use a custom method and add it via componentRefAction.

Run the following using .UseMauiReactorApp<StartPage>():

public class StartPage : Component
{
    public override VisualNode Render() => NavigationPage([
        ContentPage([
            Button("Go to Tabbed Page")
                .OnClicked(async () => await Navigation!.PushAsync<MyTabbedPage>())
        ])
    ]);
}

public class MyTabbedPage : Component
{
    public override VisualNode Render() => TabbedPage([
        ToolbarItem("Test"),

        new FirstPage(),
        new SecondPage()
    ]);

    // Workaround is to add ToolbarItem using componentRefAction and not add if one was previously added.
    //
    /*MauiControls.ToolbarItem AddToolbarItem(MauiControls.Page page, string text, EventHandler? clicked = null)
    {
        if (!page.ToolbarItems.Any(i => i.Text == text))
        {
            var toolbarItem = new MauiControls.ToolbarItem
            {
                Text = text
            };

            if (clicked != null)
                toolbarItem.Clicked += clicked;

            page.ToolbarItems.Add(toolbarItem);

            return toolbarItem;
        }

        return null!;
    }*/
}

public class FirstPage : Component
{
    public override VisualNode Render() => ContentPage([
        Label("Page 1")
    ])
    .Title("Page 1");
}

public class SecondPage : Component
{
    public override VisualNode Render() => ContentPage([
        Label("Page 2")
    ])
    .Title("Page 2");
}