DEV Community

Cover image for Responsive Flyout in .NET MAUI
Victor Hugo Garcia
Victor Hugo Garcia

Posted on • Edited on

10

Responsive Flyout in .NET MAUI

NET Multi-platform App UI (.NET MAUI) is a cross-platform framework for creating native mobile and desktop apps with C# and XAML. Using .NET MAUI, you can develop apps that can run on Android, iOS, macOS, and Windows from a single shared code-base. In this post, I will show you how to implement a responsive Flyout in your .NET MAUI app using Microsoft.Maui.Devices class by detecting when the screen size changes for iOS and Android.


Essentials

In .NET MAUI the Xamarin.Essentials were natively integrated, so you can get all the benefits along with improvements for the DeviceDisplay class.

Responsive Layout

In the ContentPage we attach an event handler to catch up screen size changes occurring to that particular page, so we render the proper Flyout behavior based on the screen size

    protected override void OnAppearing()
    {
        base.OnAppearing();
        SetFlyoutBehavior();
        DeviceDisplay.MainDisplayInfoChanged += DeviceDisplay_MainDisplayInfoChanged;
    }

    private void DeviceDisplay_MainDisplayInfoChanged(object sender, DisplayInfoChangedEventArgs e)
    {
        SetFlyoutBehavior();
    }

    protected override void OnDisappearing()
    {
        base.OnDisappearing();
        DeviceDisplay.MainDisplayInfoChanged -= DeviceDisplay_MainDisplayInfoChanged;
    }

    private void SetFlyoutBehavior()
    {
        // Get the screen points 
        double screenWidth = DeviceDisplay.MainDisplayInfo.Width / DeviceDisplay.MainDisplayInfo.Density;

        Debug.WriteLine(screenWidth);
        // sizes obtained from the official bootstrap CSS 
        switch (screenWidth)
        {
            case <= 576:
                Shell.Current.FlyoutBehavior = FlyoutBehavior.Flyout;
                Shell.Current.FlyoutIsPresented = false;
                break;
            case > 576:
                Shell.Current.FlyoutBehavior = FlyoutBehavior.Locked;
                Shell.Current.FlyoutIsPresented = true;
                break;
        }
    }
Enter fullscreen mode Exit fullscreen mode

Conclusion

This is just a sample of how to set the Flyout from App Shell to be locked or hidden based on the screen size, however, you can use it to accommodate other areas of the screen layout.

I hope this helps others to create their own responsive layouts in .NET MAUI.

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read full post →

Top comments (1)

Collapse
 
mkgungor profile image
Murat Gungor

Thank you very much for sharing this useful blog post. I was able to do the same thing for .NET MAUI WebView to set the WidthRequest.

This website also helps you to decide desired Width and Height
responsiveviewer.org/

Thank you again,

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more