question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cross-platform solution for Right click context menu on desktop app

See original GitHub issue

Context-menu support in .NET MAUI

A context menu, often known as a right-click menu, offers contextual commands that are specific to the element being clicked on. In .NET MAUI a context menu can be added to any control that is a View, which includes all the controls that “take up space on the screen,” such as Label, Entry, Image, Button, WebView, and many others.

Target platforms

  • Windows: Fully supported
  • MacCatalyst: Fully supported
  • Other platforms: We will consider Android and iOS support at a later time

Note that even on supported platforms there are differences due to what each platform supports in their native context menu support.

New APIs

The main new API is a new ContextFlyout property on the View base type, via a new IContextFlyoutContainer interface. The pattern is very similar to how a MenuBarItem contains various flyout menu items.

Compatible menu flyout types and properties

Type Windows properties MacCatalyst properties
MenuFlyoutItem for menu items you can click Text, IconImageSource, Clicked, Command[Parameter] Text, ~IconImageSource~, Clicked, Command[Parameter]
MenuFlyoutSubItem to contain sub-menu items Text Text
MenuFlyoutSeparator for horizontal lines N/A N/A

Usage examples and screenshots

A simple context menu to enable right-click support on a label has XAML like this:

    <Label Text="Right-click to pick my color" x:Name="ColorLabel">
        <Label.ContextActions>
            <MenuFlyoutItem Text="Make label red" Clicked="MakeLabelRed"></MenuFlyoutItem>
            <MenuFlyoutItem Text="Make label blue" Clicked="MakeLabelBlue"></MenuFlyoutItem>
        </Label.ContextActions>
    </Label>

And some C# code to handle the events like this:

	private void MakeLabelRed(object sender, EventArgs e) => ColorLabel.BackgroundColor = Colors.Red;
	private void MakeLabelBlue(object sender, EventArgs e) => ColorLabel.BackgroundColor = Colors.LightBlue;

And this is what it looks like on Windows:

image

And slightly more advanced with sub-menus and icons in XAML:

    <Label Text="Right-click to pick my color" x:Name="ColorLabel">
        <Label.ContextActions>
            <MenuFlyoutItemText="Make label red" Clicked="MakeLabelRed">
                <MenuFlyoutItem.IconImageSource>
                    <FontImageSource Glyph="X" FontFamily="Arial" Color="Red" />
                </MenuFlyoutItem.IconImageSource>
            </MenuFlyoutItem>
            <MenuFlyoutItem Text="Make label blue" Clicked="MakeLabelBlue">
                <MenuFlyoutItem.IconImageSource>
                    <FontImageSource Glyph="X" FontFamily="Arial" Color="Blue" />
                </MenuFlyoutItem.IconImageSource>
            </MenuFlyoutItem>
            <MenuFlyoutSubItem Text="More colors">
                <MenuFlyoutItem Text="Hot pink" Clicked="MakeLabelHotPink">
                    <MenuItem.IconImageSource>
                        <FontImageSource Glyph="X" FontFamily="Arial" Color="HotPink" />
                    </MenuItem.IconImageSource>
                </MenuFlyoutItem>
                <MenuFlyoutItem Text="Pale Goldenrod" Clicked="MakeLabelPaleGoldenrod">
                    <MenuItem.IconImageSource>
                        <FontImageSource Glyph="X" FontFamily="Arial" Color="PaleGoldenrod" />
                    </MenuItem.IconImageSource>
                </MenuFlyoutItem>
            </MenuFlyoutSubItem>
        </Label.ContextActions>
    </Label>

And similar C# event handlers:

	private void MakeLabelRed(object sender, EventArgs e) => ColorLabel.BackgroundColor = Colors.Red;
	private void MakeLabelBlue(object sender, EventArgs e) => ColorLabel.BackgroundColor = Colors.LightBlue;
	private void MakeLabelHotPink(object sender, EventArgs e) => ColorLabel.BackgroundColor = Colors.HotPink;
	private void MakeLabelPaleGoldenrod(object sender, EventArgs e) => ColorLabel.BackgroundColor = Colors.PaleGoldenrod;

And you get this lovely user interface on Windows:

image

And similarly on MacOS (note that there are no icons supported):

image

Custom context menus with Entry (textbox)

  • On Windows, this works automatically
  • On MacOS this does not yet work (you always get the default MacOS menu with clipboard functions, etc.)

Custom context menus with WebView

  • On Windows you need code like this to disable WebView2’s default menu:
                        // In the constructor, after calling InitializeComponent();
    #if WINDOWS
    			ContextMenuWebView.HandlerChanged += OnWebViewHandlerChanged;
    #endif
    
    #if WINDOWS
    	void OnWebViewHandlerChanged(object sender, EventArgs e)
    	{
    		if (ContextMenuWebView.Handler != null)
    		{
    			var webView2 = (Microsoft.UI.Xaml.Controls.WebView2)ContextMenuWebView.Handler.PlatformView;
    			webView2.CoreWebView2Initialized += OnWebView2CoreWebView2Initialized;
    		}
    	}
    
    	private void OnWebView2CoreWebView2Initialized(Microsoft.UI.Xaml.Controls.WebView2 sender, Microsoft.UI.Xaml.Controls.CoreWebView2InitializedEventArgs args)
    	{
    		sender.CoreWebView2.Settings.AreDefaultContextMenusEnabled = false;
    	}
    #endif
    
  • On MacOS this is being investigated.

Custom context menu location

Not yet supported. The context menu shows up exactly where the pointer was right-clicked, and opens in the appropriate orientation for the device.

Dynamic context menu items

Changing the contents of the context menus dynamically is not fully supported at this time. The code in the PR contains some comments to that effect. It should be implemented in both Windows and macOS.

Using {Binding ...} for context menu items

There are some limitations with binding in menu items. This is related to the following issues:

Original Description

Description

Right-click context menus are a very very important feature for desktop apps. Almost every desktop app needs this feature. But I don’t think there is a cross-platform way to do it in MAUI.

Public API Changes

Have to introduce a new control for the context menu like other desktop frameworks.

Intended Use-Case

This can be very useful for providing desktop-specific options or when you are building a desktop-only app with MAUI

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:9
  • Comments:17 (9 by maintainers)

github_iconTop GitHub Comments

6reactions
Eiloncommented, Jul 21, 2022

It’s hardly even a prototype, but I think we can make this work! At least, on Windows for now!

I hard-coded a ton of stuff, but this is a right-click context menu on a label:

screenshot of a .NET MAUI app with context menus on arbitrary items
4reactions
msftbot[bot]commented, Jul 21, 2022

We’ve moved this issue to the Future milestone. This means that it is not going to be worked on for the coming release. We will reassess the issue following the current release and consider this item at that time.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do you add an option to the right-click context menu in ...
To add an option to the right-click context menu in Windows Explorer, you can follow these steps: 1. Open the Registry Editor by...
Read more >
How to Add Apps to the Windows Right Click Context Menu
If you want a quick way to open your programs or apps, you can add a right click shortcut to your Windows 10...
Read more >
android - Adding new menu item to right-click (all platforms)
No, there is no cross-platform way to do this. In Windows, you edit the registry. In Android, you can implement this in as...
Read more >
Display a context menu in a .NET MAUI desktop app
A context menu, often known as a right-click menu, offers contextual commands that are specific to the control being clicked on. In .NET...
Read more >
Windows 11: right click => more options : r/Windows11
Why. Every time i open a context menu i have to press more options. Why does microsoft think that this is a good...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found