Xamarin.forms: [Spec] ContextMenu

Created on 11 Apr 2019  Â·  10Comments  Â·  Source: xamarin/Xamarin.Forms

Related to CollectionView

ContextItem

ContextItem provides a way to specify a contextual interaction, often displayed as part of a context menu.

public class ContextItem
{
    public static readonly BindableProperty TextProperty;
    public string Text { get; set; }

    public static readonly BindableProperty CommandProperty;
    public ICommand Command { get; set; }

    public static readonly BindableProperty CommandParameterProperty;
    public object CommandParameter { get; set; }

    public static readonly BindableProperty IsEnabledProperty;
    public bool IsEnabled { get; set; }

    public static readonly BindableProperty IconProperty;
    public ImageSource Icon { get; set; }

    public event EventHandler Invoked;
}

Properties

| API | Description |
| ------------- | ------------- |
| Text | Gets or sets the text description displayed on the item. |
| Command | Gets or sets the command to execute when this item is invoked. |
| CommandParameter | |
| IsEnabled | Gets or sets a value indicating whether the user can interact with the context item. |
| Icon | Gets or sets the graphic content of the item. |

Events

| API | Description |
| ------------- | ------------- |
| Invoked | Occurs when user interaction indicates that the command represented by this item should execute. |

ContextMenu

Provides a way to specify a set of interactions to be displayed in a context menu. A ContextMenu can be provided for any VisualElement, and is activated/displayed according to the native platform conventions.

public static class ContextMenu
{
    public static readonly BindableProperty MenuItemsProperty =
            BindableProperty.CreateAttached("MenuItems", typeof(ContextMenuItems), typeof(VisualElement));
}


public class ContextMenuItems : IList<ContextItem>
{
}
proposal-accepted enhancement âž•

Most helpful comment

@hartez in general, need the flexibility to hide a menu item in the case a user gains/loses access to feature while the app is running without having to rebuild the context menu. Same philosophy that would be used for toolbar items. From a view model, I should be able to hide an item so a user never sees it until the right conditions are met and also change the enabled stated based on a different set of conditions. Visibility is normally used for authorization scenarios and the enabled state is used to control command process.

All 10 comments

could IsVisible be added to the spec?

@powerdude On ContextItem, right?

@samhouts yes, sorry for missing that detail. I think wherever there is IsEnabled, there should also be an IsVisible.

@hartez Any reason we shouldn't include IsVisible?

@powerdude Could you elaborate on that? What's a situation where you would want to have an invisible context item?

@hartez in general, need the flexibility to hide a menu item in the case a user gains/loses access to feature while the app is running without having to rebuild the context menu. Same philosophy that would be used for toolbar items. From a view model, I should be able to hide an item so a user never sees it until the right conditions are met and also change the enabled stated based on a different set of conditions. Visibility is normally used for authorization scenarios and the enabled state is used to control command process.

@powerdude @samhouts That seems pretty reasonable. At the moment I can't see any reason not to include IsVisible.

@samhouts @hartez Is there a specific reason why we are creating a new class ContextMenuItems instead of directly using IList<ContextItem> in the bindable property type for MenuItemsProperty?

Also, have you thought about nested context menus, inline menus, and destructive context items?

As @adrianknight89 mentioned, I wonder if we wouldn't be better off reusing an existing class, such as MenuItem, for this? Maybe not; I'm not super familiar with the Xamarin.Forms internals.

Also, I agree with @powerdude that it would be nice to have an IsVisible property. I'm guessing that if the context menu is already open and a context menu item's IsVisible property changes, then that item probably wouldn't be hidden/shown until after the user right clicked again, which I think is fine :)

Was this page helpful?
0 / 5 - 0 ratings