Xamarin在单击时形成显示上下文菜单

时间:2017-09-11 15:22:14

标签: c# xamarin.forms

我设法通过长按来创建上下文菜单,尝试使用C#代码中的listview上的单个工具来使其工作。

SAMPLE XAML

<?xml version="1.0" encoding="utf-8" ?>
 <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:local="clr-namespace:App2.Services"
         x:Class="App2.Views.Clients.ClientsPage">
<ContentPage.Content>

    <ListView x:Name="clientList" ItemsSource="{x:Static 
local:ClientService.ClientsGrouping}"
              IsGroupingEnabled="True"
              HasUnevenRows="True"
              SeparatorVisibility="Default"
              GroupShortNameBinding="{Binding GroupByShortName}">
        <ListView.GroupHeaderTemplate>
            <DataTemplate>
                <!--<TextCell Text="{Binding GroupByTitle}" />-->
                <ViewCell Height="30">
                    <ContentView>
                        <Label FontSize="Medium" FontFamily="bold" Text="
{Binding GroupByTitle}" BackgroundColor="LightGray"/>
                    </ContentView>
                </ViewCell>
            </DataTemplate>
        </ListView.GroupHeaderTemplate>
        <ListView.ItemTemplate>
            <DataTemplate>

                <!--<TextCell Text="{Binding FullName}" Detail="{Binding 
Email}" />-->

                <ViewCell Height="100">

                    <ViewCell.ContextActions>
                        <MenuItem x:Name="callAction" Text="Call" 
Clicked="callAction_Clicked" />
                        <MenuItem x:Name="emailAction" Text="Email" 
Clicked="emailAction_Clicked" />
                        <MenuItem x:Name="viewProfileAction" Text="Profile" 
 Clicked="viewProfileAction_Clicked" />
                    </ViewCell.ContextActions>

                    <ContentView Margin="10">

                        <Grid RowSpacing="0">

                            <Grid.RowDefinitions>
                                <RowDefinition Height="*" />
                                <RowDefinition Height="*" />
                            </Grid.RowDefinitions>

                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="2*" />
                                <ColumnDefinition Width="50" />
                            </Grid.ColumnDefinitions>


                            <StackLayout Orientation="Vertical"  
Grid.Row="0" Grid.Column="0">
                                <Label  Text="{Binding FullName}"></Label>
                            </StackLayout>
                            <Label Text="{Binding Email}" Grid.Row="1" 
Grid.Column="0"></Label>
                            <!--<Button x:Name="clientContextMenu" 
Text="..." Grid.Row="0" Grid.Column="1" Clicked="clientContextMenu_Clicked">
</Button>-->
                            <Image Source="menu_more.png" Grid.Row="0" 
Grid.Column="1"/>
                        </Grid>
                    </ContentView>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</ContentPage.Content>
</ContentPage>

我知道如何触发列表视图项目所选事件,但是在单击项目时显示上下文菜单并不确定如何实现该目标。

1 个答案:

答案 0 :(得分:1)

 <Viewcell.ContextActions>
                    <MenuItem x:Name="MenuItem"  Clicked="OnClicked" Text="{Binding Text}"   CommandParameter="{Binding Name}" IsDestructive=false/>
                  </Viewcell.ContextActions>

您是否尝试过滑动菜单项,然后单击而不是执行操作。