Menus in FmgLib.MauiMarkup

Version: v9.x

Context menu

Here is an example of creating a context menu for an image. The context menu has options for copying and pasting, and also for changing the background color of a grid.

new Grid()
.Assign(out var grid)
.Children(
    new Image()
	    .Source("dotnet_bot.png")
        .ContextFlyout(new MenuFlyout()
			{
	            new MenuFlyoutItem()
	            .Text("Copy")
				.OnClicked(e => Console.WriteLine("Copy")),
	            new MenuFlyoutItem()
	            .Text("Paste")
	            .OnClicked(e => Console.WriteLine("Paste")),
	            new MenuFlyoutSubItem()
	            {
	                new MenuFlyoutItem()
	                .Text("Blue")
	                .OnClicked(e => grid.BackgroundColor = Colors.Blue),
	                new MenuFlyoutItem()
	                .Text("Red")
	                .OnClicked(e => grid.BackgroundColor = Colors.Red),
	                new MenuFlyoutItem()
	                .Text("Black")
	                .OnClicked(e => grid.BackgroundColor = Colors.Black)
	            }
	            .Text("Background color")
	        }
        )
)

Here is an example of creating a menu bar for a ContentPage. The menu bar has three options: My Menu, Edit, and Theme.

public class MenuPage : ContentPage
{
    public MenuPage()
    {
        this.MenuBarItems(new MenuBarItem[]
        {
            new MenuBarItem()
            {
                new MenuFlyoutItem()
		            .Text("Exit")
                    .OnClicked(e => Application.Current.Quit()),
            }
            .Text("My Menu"),
            new MenuBarItem()
            {
                new MenuFlyoutItem()
		            .Text("Copy")
                    .OnClicked(e => Console.WriteLine("Copy"))
                    .KeyboardAccelerators(
                        new KeyboardAccelerator()
                        .Key("C")
                        .Modifiers(KeyboardAcceleratorModifiers.Ctrl)
                    ),
                new MenuFlyoutItem()
		            .Text("Paste")
                    .OnClicked(e => Console.WriteLine("Paste"))
                    .KeyboardAccelerators(
                        new KeyboardAccelerator()
                        .Key("V")
                        .Modifiers(KeyboardAcceleratorModifiers.Ctrl)
                    ),
            }
            .Text("Edit"),
            new MenuBarItem()
            {
                new MenuFlyoutItem()
	                .Text("Blue")
                    .OnClicked(e => this.BackgroundColor = Colors.Blue),
                ...
            }
            .Text("Theme")
        });

        ...
    }
}