简体   繁体   中英

dotNet MAUI Shell - Add Flyout Item in C#

I have an old App I wrote years ago in Xamarin and looking to update it to MAUI, and not a huge fan of writing Xaml.

I know you can render pages with straight code, so I thought I would try this with Shell as well, but can't figure out the correct context for adding flyout items.

Even the slightest attempt like the below causes unhandled win32 errors:

public partial class AppShell : Shell
{
    public AppShell()
    {
        Items.Add(new FlyoutItem
        {
            Title = "test"
        });
    }
}

So I'm obviously going about it the wrong way. What is the correct syntax?

在此处输入图像描述

Okay, so I figured it out myself. I found this page which put me onto the right path: https://gist.github.com/TheBaileyBrew/f8a9d2e4668da3ec9bff9bf86d32d951

Anyway, so basically I was able to create an XAMLess AppShell.cs with essentially only this and it created the Shell instance, no problem.

public partial class AppShell : Shell
{
    public AppShell()
    {
        Routing.RegisterRoute(nameof(MainPage), typeof(MainPage));

        Items.Add(new FlyoutItem
        {
            Title = "Home",
            Route = x.Route,
            Icon = new FontImageSource
            {
                FontFamily = "fasolid900",
                Glyph = FontAwesome.FontAwesomeIcons.Home,
            },
            Items =
            {
                new Tab{
                    Title = "Home",
                    Items = {
                        new ShellContent
                        {
                            Title = "Home",
                            Route = nameof(MainPage),
                            ContentTemplate = new DataTemplate(typeof(MainPage))
                        }
                    }
                }
            }
        });
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM