Home > Mobile >  how to add a context menu to a list item in xamarin UWP app?
how to add a context menu to a list item in xamarin UWP app?

Time:10-13

I have the following xaml code:

 <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:local="clr-namespace:GraphTutorial.Models"
         Title="Shared Document Library"
         x:Class="GraphTutorial.SPDocumentLibraryContentsPage">

    ...
    ...

     <ListView x:Name="SharedDocumentList"
                  HasUnevenRows="true"
                  Margin="10,10,10,10"
                  ItemSelected="OnItemSelected">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <StackLayout Margin="10,10,10,10">
                            <Label Text="{Binding Path=Id}"
                                   FontAttributes="Bold"
                                   FontSize="Medium" />
                            <Label Text="{Binding Path=WebUrl}"
                                   FontSize="Small" />
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
            <ListView.Resources>
                <MenuFlyout x:Name="DocumentActions">
                    <FlyoutItem x:Name="Edit"  Text="Edit" />
                    <FlyoutItem x:Name="Remove" Text="Remove"    Click="Remove_Click" />
                </MenuFlyout>
            </ListView.Resources>
        </ListView>

I'm presently getting the following error message on the line:

Error XLS0414 The type 'MenuFlyout' was not found. Verify that you are not missing an assembly reference and that all referenced assemblies have been built.

Can someone point me in the right direction?

Thanks.

EDIT 1

I also have tried this:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:local="clr-namespace:GraphTutorial.ContextMenu"  
         Title="Shared Document Library"
         x:Class="GraphTutorial.SPDocumentLibraryContentsPage">

        <ListView x:Name="SharedDocumentList"
                  HasUnevenRows="true"
                  Margin="10,10,10,10"
                  ItemSelected="OnItemSelected">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <StackLayout Margin="10,10,10,10">
                            <Label Text="{Binding Path=Id}"
                                   FontAttributes="Bold"
                                   FontSize="Medium" />
                            <Label Text="{Binding Path=WebUrl}"
                                   FontSize="Small" />
                        </StackLayout>
                    </ViewCell>
                    <ViewCell.ContextActions>
                        <MenuItem Text="Add" Clicked="Add_Clicked"></MenuItem>
                        <MenuItem Text="Delete" Clicked="Delete_Clicked"></MenuItem>
                        <MenuItem Text="Edit" Clicked="Edit_Clicked">
                    </ViewCell.ContextActions>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

But this returns the error:

XLS0415 The attachable property 'ContextActions' was not found in type 'ViewCell'.

CodePudding user response:

Error XLS0414 The type 'MenuFlyout' was not found. Verify that you are not missing an assembly reference and that all referenced assemblies have been built.

From document MenuFlyout Class,we know that class MenuFlyout is a class in uwp, so we can't use it in xamarin.

If you want to add a context menu to a list item in xamarin UWP,you can refer to the following code:

<ListView x:Name="listView" Margin="20" ItemSelected="OnListItemSelected">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <StackLayout Margin="20,0,0,0" Orientation="Horizontal" HorizontalOptions="FillAndExpand">
                    <Label Text="{Binding Name}" VerticalTextAlignment="Center" HorizontalOptions="StartAndExpand" />
                    <Image Source="check.png" HorizontalOptions="End" IsVisible="{Binding Done}" />
                </StackLayout>

                <ViewCell.ContextActions>
                    <MenuItem Text="Delete"
                                  Clicked="OnDeleteClicked"/>
                </ViewCell.ContextActions>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

In page.xaml.cs

    void OnDeleteClicked(object sender, EventArgs e)
    {
        TodoItem itemToDelete = ((sender as MenuItem).BindingContext as TodoItem);
        // other code
    }

For more detail, you can check:

https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/menuitem

https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/menuitem#cross-platform-context-menu-behavior

  • Related