Creating a Menu Bar UI with .NET MAUI Preview 14 | Syncfusion Blogs
Live Chat Icon For mobile
Live Chat Icon
Popular Categories.NET  (172).NET Core  (29).NET MAUI  (192)Angular  (107)ASP.NET  (51)ASP.NET Core  (82)ASP.NET MVC  (89)Azure  (40)Black Friday Deal  (1)Blazor  (209)BoldSign  (12)DocIO  (24)Essential JS 2  (106)Essential Studio  (200)File Formats  (63)Flutter  (131)JavaScript  (219)Microsoft  (118)PDF  (80)Python  (1)React  (98)Streamlit  (1)Succinctly series  (131)Syncfusion  (882)TypeScript  (33)Uno Platform  (3)UWP  (4)Vue  (45)Webinar  (49)Windows Forms  (61)WinUI  (68)WPF  (157)Xamarin  (161)XlsIO  (35)Other CategoriesBarcode  (5)BI  (29)Bold BI  (8)Bold Reports  (2)Build conference  (8)Business intelligence  (55)Button  (4)C#  (146)Chart  (125)Cloud  (15)Company  (443)Dashboard  (8)Data Science  (3)Data Validation  (8)DataGrid  (62)Development  (613)Doc  (7)DockingManager  (1)eBook  (99)Enterprise  (22)Entity Framework  (5)Essential Tools  (14)Excel  (37)Extensions  (22)File Manager  (6)Gantt  (18)Gauge  (12)Git  (5)Grid  (31)HTML  (13)Installer  (2)Knockout  (2)Language  (1)LINQPad  (1)Linux  (2)M-Commerce  (1)Metro Studio  (11)Mobile  (488)Mobile MVC  (9)OLAP server  (1)Open source  (1)Orubase  (12)Partners  (21)PDF viewer  (41)Performance  (12)PHP  (2)PivotGrid  (4)Predictive Analytics  (6)Report Server  (3)Reporting  (10)Reporting / Back Office  (11)Rich Text Editor  (12)Road Map  (12)Scheduler  (52)Security  (3)SfDataGrid  (9)Silverlight  (21)Sneak Peek  (31)Solution Services  (4)Spreadsheet  (11)SQL  (10)Stock Chart  (1)Surface  (4)Tablets  (5)Theme  (12)Tips and Tricks  (112)UI  (368)Uncategorized  (68)Unix  (2)User interface  (68)Visual State Manager  (2)Visual Studio  (30)Visual Studio Code  (17)Web  (577)What's new  (313)Windows 8  (19)Windows App  (2)Windows Phone  (15)Windows Phone 7  (9)WinRT  (26)
Creating a Menu Bar UI with .NET MAUI Preview 14

Creating a Menu Bar UI with .NET MAUI Preview 14

On March 16, 2022, Microsoft released .NET MAUI Preview 14. With this new preview 14 release, .NET MAUI offers a menu bar UI, which is typically a menu that can be placed at the top of desktop apps on Windows. This menu bar can be seen as the title bar for macOS. I enjoyed working with this menu bar in my .NET MAUI application using .NET MAUI Preview 14.

In this blog, I am going to create a photo viewer and editor .NET MAUI desktop application and integrate the menu bar in it.

Create photo viewer and editor app

Step 1: First, create a simple .NET MAUI Preview 14 application using Visual Studio.

Step 2: Menus can only appear on Shell and NavigationPage. For this example, I am choosing to use the NavigationPage to display the menu. So, load the MainPage as the NavigationPage.

public partial class App : Application
{
  public App()
  {
    InitializeComponent();MainPage = new NavigationPage(new MainPage());
  }
}

Syncfusion’s .NET MAUI controls suite is the expert’s choice for building modern web apps.

Step 3: Now, declare the MenuBarItems collection in the Content page to display the menu.

<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="MenuBarUIApplication.MainPage"
             BackgroundColor="{DynamicResource SecondaryColor}">
  <ContentPage.MenuBarItems></ContentPage.MenuBarItems>
</ContentPage>

Step 4: Then, add the necessary items to the MenuBarItems collection to display them on the menu. In this photo viewer and editor application, I am going to add File and Edit menus. File menu will contain the options Open, Save, and Exit, and the Edit menu will contain Rotate, Zoom, and Reset.

Refer to the following code example.

<ContentPage.MenuBarItems>
 <MenuBarItem Text="File">
  <MenuFlyoutItem Text="Open" Clicked="MenuFlyoutItem_Clicked" />
  <MenuFlyoutItem Text="Save" />
  <MenuFlyoutItem Text="Exit" />
 </MenuBarItem>
 <MenuBarItem Text="Edit">
  <MenuFlyoutSubItem Text="Rotate">
  </MenuFlyoutSubItem>
  <MenuFlyoutItem Text="Zoom" Clicked="MenuFlyoutItem_Clicked_5" />
  <MenuFlyoutItem Text="Reset" Clicked="MenuFlyoutItem_Clicked_6" />
 </MenuBarItem>
</ContentPage.MenuBarItems>

Step 5: Now let’s add cascading menu items to the MenuBarItem. First, I add a submenu to the Rotate MenuBarItem. The Rotate menu will have the options Rotate 90, Rotate 180, Rotate 270, and Rotate 360 as cascading menus.

Refer to the following code.

<ContentPage.MenuBarItems>
 <MenuBarItem Text="File">
  <MenuFlyoutItem Text="Open" Clicked="MenuFlyoutItem_Clicked" />
  <MenuFlyoutItem Text="Save" />
  <MenuFlyoutItem Text="Exit" />
 </MenuBarItem>
 <MenuBarItem Text="Edit">
   <MenuFlyoutSubItem Text="Rotate">
    <MenuFlyoutItem Text="Rotate 90" Clicked="MenuFlyoutItem_Clicked_1"/>
    <MenuFlyoutItem Text="Rotate 180" Clicked="MenuFlyoutItem_Clicked_2"/>
    <MenuFlyoutItem Text="Rotate 270" Clicked="MenuFlyoutItem_Clicked_3"/>
    <MenuFlyoutItem Text="Rotate 360" Clicked="MenuFlyoutItem_Clicked_4"/>
   </MenuFlyoutSubItem>
   <MenuFlyoutItem Text="Zoom" Clicked="MenuFlyoutItem_Clicked_5" />
   <MenuFlyoutItem Text="Reset" Clicked="MenuFlyoutItem_Clicked_6" />
 </MenuBarItem>
</ContentPage.MenuBarItems>

Every property of the Syncfusion .NET MAUI controls is completely documented to make it easy to get started.

Step 6: Now, add an Image control as the content of the ContentPage within a grid to clip the bounds. The Image control can be used to view and edit the photos.

Refer to the following code example.

<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="MenuBarUIApplication.MainPage"
             BackgroundColor="{DynamicResource SecondaryColor}">
  <ContentPage.MenuBarItems>
   <MenuBarItem Text="File">
    <MenuFlyoutItem Text="Open" Clicked="MenuFlyoutItem_Clicked" />
    <MenuFlyoutItem Text="Save" />
    <MenuFlyoutItem Text="Exit" />
   </MenuBarItem>
   <MenuBarItem Text="Edit">
    <MenuFlyoutSubItem Text="Rotate">
     <MenuFlyoutItem Text="Rotate 90" Clicked="MenuFlyoutItem_Clicked_1"/>
     <MenuFlyoutItem Text="Rotate 180" Clicked="MenuFlyoutItem_Clicked_2"/>
     <MenuFlyoutItem Text="Rotate 270" Clicked="MenuFlyoutItem_Clicked_3"/>
     <MenuFlyoutItem Text="Rotate 360" Clicked="MenuFlyoutItem_Clicked_4"/>
    </MenuFlyoutSubItem>
    <MenuFlyoutItem Text="Zoom" Clicked="MenuFlyoutItem_Clicked_5" />
    <MenuFlyoutItem Text="Reset" Clicked="MenuFlyoutItem_Clicked_6" />
   </MenuBarItem>
  </ContentPage.MenuBarItems><Grid IsClippedToBounds="True">
  <Image x:Name="photoViewerImage"
         Source="dotnet_bot.png"
         HorizontalOptions="Center" />
  </Grid>
</ContentPage>

Step 7: Last, add the code-behind logic for the MenuBarItems to perform their respective actions. I am using the FilePicker from .NET MAUI Preview 14, which offers a UI to pick files in desktop platforms. Also, I am using the ViewExtensions.RotateTo and ViewExtensions.ScaleTo methods of the Image control to perform rotate and zoom actions, respectively.

private async void MenuFlyoutItem_Clicked(object sender, EventArgs e)
{
  var result = await OpenPicker(PickOptions.Images);
}async Task<FileResult> OpenPicker(PickOptions imageOption)
{
try
{
  var value = await FilePicker.PickAsync(imageOption);
  if (value != null)
  {
    if (value.FileName.EndsWith("png", StringComparison.OrdinalIgnoreCase) ||
    value.FileName.EndsWith("jpg", StringComparison.OrdinalIgnoreCase))
    {
      var stream = await value.OpenReadAsync();
      this.photoViewerImage.Source = ImageSource.FromStream(() => stream);
    }
  }return value;
}
catch (Exception ex)
{
}return null;
}private void MenuFlyoutItem_Clicked_1(object sender, EventArgs e)
{
  this.photoViewerImage.RotateTo(90, 500, Easing.BounceIn);
}private void MenuFlyoutItem_Clicked_2(object sender, EventArgs e)
{
  this.photoViewerImage.RotateTo(180, 500, Easing.BounceIn);
}
private void MenuFlyoutItem_Clicked_3(object sender, EventArgs e)
{
  this.photoViewerImage.RotateTo(270, 500, Easing.BounceIn);
}
private void MenuFlyoutItem_Clicked_4(object sender, EventArgs e)
{
  this.photoViewerImage.RotateTo(360, 500, Easing.BounceIn);
}
private void MenuFlyoutItem_Clicked_5(object sender, EventArgs e)
{
  this.photoViewerImage.ScaleTo(2, 500, Easing.BounceOut);
}
private void MenuFlyoutItem_Clicked_6(object sender, EventArgs e)
{
  this.photoViewerImage.ScaleTo(1, 500, Easing.BounceOut);
  this.photoViewerImage.RotateTo(0, 500, Easing.BounceIn);
}

Now, the simple .NET MAUI Preview 14 Photo Viewer and Editor is ready, with menus using MenuBarItems.

MenuBarItem using .NET MAUI Preview 14
MenuBarItem using .NET MAUI Preview 14

Syncfusion .NET MAUI controls allow you to build powerful line-of-business applications.

GitHub reference

Also, you can check out our full example of Creating a Menu Bar UI with .NET MAUI Preview 14 on GitHub.

Syncfusion compatibility with .NET MAUI Preview 14

Syncfusion .NET MAUI controls will be compatible with .NET MAUI Preview 14 in our Essential Studio for .NET MAUI 2022 Volume 1 release. This release is expected to be rolled out by the end of March. You can install our control package from NuGet Gallery and use it in your .NET MAUI Preview 14 application then.

Conclusion

I hope you enjoyed this blog and thanks for reading! For more details, refer to the article, .NET MAUI Preview 14. Also, check out the .NET MAUI Preview 14 release notes.

If you have any feedback, special requirements, or controls that you’d like to see in our .NET MAUI suite, please let us know in the comments section below.

Also, you can contact us through our support forum, support portal, or feedback portal. We are always happy to assist you!

Test Flight
App Center Badge
Google Play Store Badge
Microsoft Badge
Github Store Badge

Related blogs

Tags:

Share this post:

Popular Now

Be the first to get updates

Subscribe RSS feed

Be the first to get updates

Subscribe RSS feed
Scroll To Top