Flyout Page in .NET MAUI

Introduction

.NET MAUI, a cross-platform framework, empowers developers to build native mobile and desktop applications using C# and XAML. It enables the creation of apps that seamlessly operate on Android, iOS, macOS, and Windows, all from a unified codebase. This open-source platform is an advancement of Xamarin Forms, expanding its reach to desktop scenarios while enhancing UI controls for improved performance and extensibility.

In this article, we will see how we can implement Flyout Page in .NET MAUI project.

Project Setup

Implementation

To create a flyout page in a Xamarin.Forms application, you can follow these steps:

Full Code

namespace MauiFlyoutPage;

public partial class App : Application
{
	public App()
	{
		InitializeComponent();

		MainPage = new AppFlyoutPage();
	}
}
<FlyoutPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
            xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
            xmlns:local="clr-namespace:MauiFlyoutPage"
            x:Class="MauiFlyoutPage.AppFlyoutPage">
    <FlyoutPage.Flyout>
        <local:MenuPage x:Name="flyoutPage" />
    </FlyoutPage.Flyout>
    <FlyoutPage.Detail>
        <NavigationPage>
            <x:Arguments>
                <local:FirstPage />
            </x:Arguments>
        </NavigationPage>
    </FlyoutPage.Detail>
</FlyoutPage>
namespace MauiFlyoutPage;

public partial class AppFlyoutPage : FlyoutPage
{
	public AppFlyoutPage()
	{
		InitializeComponent();
        flyoutPage.btn.Clicked += OpenSecondPageClicked;
    }

    private void OpenSecondPageClicked(object sender, EventArgs e)
    {
        if (!((IFlyoutPageController)this).ShouldShowSplitMode)
            IsPresented = false;
        Detail = new NavigationPage(new SecondPage());
    }
}
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="MauiFlyoutPage.FirstPage"
             Title="FirstPage">
    <VerticalStackLayout HorizontalOptions="Center"
                         VerticalOptions="Center">
        <Label 
            Text="First Page"
            VerticalOptions="Center" 
            HorizontalOptions="Center" />
    </VerticalStackLayout>
</ContentPage>
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="MauiFlyoutPage.MenuPage"
             Title="MenuPage">
    <VerticalStackLayout VerticalOptions="Center"
                         HorizontalOptions="Center">
        <Button 
            x:Name="btn"
            x:FieldModifier="public"
            Text="Open Second Page"
            VerticalOptions="Center" 
            HorizontalOptions="Center"/>
    </VerticalStackLayout>
</ContentPage>
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="MauiFlyoutPage.SecondPage"
             Title="SecondPage">
    <VerticalStackLayout VerticalOptions="Center"
                         HorizontalOptions="Center">
        <Label 
            Text="Second Page"
            VerticalOptions="Center" 
            HorizontalOptions="Center" />
    </VerticalStackLayout>
</ContentPage>

Demo

Download Code

You can download the code from GitHub. If you have any doubts, feel free to post a comment. If you liked this article and it is useful to you, do like, share the article & star the repository on GitHub.

References