XAML 如何在Xamarin中添加三点菜单

ibrsph3r  于 2022-12-07  发布在  其他
关注(0)|答案(1)|浏览(158)

我是Xamarin的新手。我想在我的应用程序中添加一个上下文菜单(类似于下图所示的三个点)。当我点击ListView的ItemTemplate中的三个点图像时,必须显示该菜单。
下面是我所期望的开始。我怎样才能在Xamarin中达到这个要求?
这方面的任何帮助都将非常有帮助。

4si2a6ki

4si2a6ki1#

你有两个选择。如果你需要,你可以使用上下文菜单来实现常规行为,如果你的需求很复杂,你可以使用开源弹出窗口来定制。
如果你是Xamarin的新手,那么我建议你采用第一种方法。

方法1:

有关ListView中的上下文菜单,请参阅此处的文档。
This documentation会更有帮助。
你必须做一些像下面这样的事情。

<ListView ItemsSource="{Binding Items}">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <ViewCell.ContextActions>
                    <MenuItem Text="Share"
                                    IconImageSource="share.png"
                                    Command="{Binding Source={x:Reference contentPage}, Path=BindingContext.ShareCommand}"
                                    CommandParameter="{Binding .}"/>
                    <MenuItem Text="Add to playlist"
                                    Command="{Binding Source={x:Reference contentPage}, Path=BindingContext.AddToPlaylistCommand}"
                                    CommandParameter="{Binding .}"/>
                </ViewCell.ContextActions>
                <Grid>
                    ...
                    <Image Source="ThreeDottedIcon" Grid.Column="2" HorizontalOptions="End">
                        <Image.GestureRecognizers>
                            <TapGestureRecognizer Command="{Binding ThreeDottedIconPopup}"/>
                        </Image.GestureRecognizers>
                    </Image>
                </Grid>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

注意:你必须根据你的需求来编写你的视图模型逻辑,我在这里没有给出。

方法2

否则,如果您希望使用开源弹出窗口,则可以使用opensource plugin进行配置。
下面的代码示例非常适合我。

XAML页面

<ListView.ItemTemplate>
    <DataTemplate>
        <ViewCell>
            ...
            <Image Source="ThreeDottedIcon" Grid.Column="2" HorizontalOptions="End">
                <Image.GestureRecognizers>
                    <TapGestureRecognizer Command="{Binding ThreeDottedIconPopup}"/>
                </Image.GestureRecognizers>
            </Image>
        </ViewCell>
    </DataTemplate>
</ListView.ItemTemplate>

XAML.cs页面

private Command _threeDottedIconPopup;
public Command ThreeDottedIconPopup
{
    get
    {
        return _threeDottedIconPopup?? (_threeDottedIconPopup= new Command(async () =>
        {
            await PopupNavigation.Instance.PushAsync(new ContextPopup(ParametersIfRequired));
        }));
    }
}

有关弹出窗口的NuGet包,请参阅link here
希望能帮上忙。

相关问题