-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cross-platform solution for Right click context menu on desktop app #7777
Comments
This is also useful on mobile apps. Considering the fact that an alternative for context menu which is swipe menu doesn't work with custom items (it's been 2 months but yet it is still included in the official docs) (#6018), this is a really needed feature. |
@janseris agreed. On mobile, it is also useful. But normally I see them used more on the desktop side. I know ios/maccatalyst has native controls for context menu. WinUi and android also might have something like that. |
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
It wouldn't be harder to do on ios and mac either. They have specific control for that. Just using that wouldn't be easy? |
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
Yeah Android will be interesting and I haven't thought about it much. My aim is to first have a good desktop experience where context menus are most expected. Once I get that working reasonably well, I'll look at iOS/iPadOS and Android. |
This comment was marked as outdated.
This comment was marked as outdated.
Context-menu support in .NET MAUIA 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 Target platforms
Note that even on supported platforms there are differences due to what each platform supports in their native context menu support. New APIsThe main new API is a new Compatible menu flyout types and properties
Usage examples and screenshotsA 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: 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: And similarly on MacOS (note that there are no icons supported): Custom context menus with Entry (textbox)
Custom context menus with WebView
Custom context menu locationNot 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 itemsChanging 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
|
@Eilon please consider Android as soon as possible |
For sure. It's all about priorities and what we can get done in time for .NET 7, so we will have to see how far we can get. |
@Qws that's an interesting idea but out-of-scope for the current feature set. Right now we're aiming to get "regular" context menus available for desktop apps. |
Add context menu (right-click menu) support for Windows and MacCatalyst. Fixes #7777
Add context menu (right-click menu) support for Windows and MacCatalyst. Fixes #7777
Add context menu (right-click menu) support for Windows and MacCatalyst. Fixes #7777
Add context menu (right-click menu) support for Windows and MacCatalyst. Fixes #7777
* Context menus for all Views Add context menu (right-click menu) support for Windows and MacCatalyst. Fixes #7777 * Add Public API info * Update PublicAPI.Unshipped.txt * - add tests and fix BC propagation * - cleanup winui handler * - add comment and issue reference * - add some additional samples for testing * - add link to github issue * - simplify ios mapcontextflyout * - cleanup * - Convert to MenuFlyout * - fix handler names * - fix test names * - inline creating of args * - add FlyoutBase API * - fix Tizen API * - fix device tests * - switch to attached property * - IContextFlyoutElement * - fixed BP * - fix propagated bindingcontext * - rewire context menu code to be more on demand ios - wire up context menu for MKWebView * Update ContextFlyoutTests.Windows.cs * Update Element.Impl.cs * - fix extra ZIndex APIs Co-authored-by: Shane Neuville <shneuvil@microsoft.com>
I was wondering if this is also related to shell context menu methods coming to MAUI – a way to specify these in a cross-platform way would be very welcome (e.g. right click a folder in windows > specific shell menus come up). |
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 asLabel
,Entry
,Image
,Button
,WebView
, and many others.Target platforms
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 theView
base type, via a newIContextFlyoutContainer
interface. The pattern is very similar to how aMenuBarItem
contains various flyout menu items.Compatible menu flyout types and properties
MenuFlyoutItem
for menu items you can clickText
,IconImageSource
,Clicked
,Command[Parameter]
Text
,,IconImageSource
Clicked
,Command[Parameter]
MenuFlyoutSubItem
to contain sub-menu itemsText
Text
MenuFlyoutSeparator
for horizontal linesUsage examples and screenshots
A simple context menu to enable right-click support on a label has XAML like this:
And some C# code to handle the events like this:
And this is what it looks like on Windows:
And slightly more advanced with sub-menus and icons in XAML:
And similar C# event handlers:
And you get this lovely user interface on Windows:
And similarly on MacOS (note that there are no icons supported):
Custom context menus with Entry (textbox)
Custom context menus with WebView
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 itemsThere 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
The text was updated successfully, but these errors were encountered: