5.列表选择弹窗(BottomListPopup)

愿你出走半生,归来仍是少年!

环境:.NET 7、MAUI

从底部弹出的列表选择弹窗。

1.布局

XML 复制代码
<?xml version="1.0" encoding="utf-8" ?>
<toolkit:Popup xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="MauiLib.Utility.Controls.Popups.BottomListPopup"
             xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
             x:Name="popup" 
             xmlns:config="clr-namespace:MauiLib.Utility.Configs;assembly=MauiLib.Utility"
             Color="Transparent" 
              VerticalOptions="End" 
                
              > 

    <VerticalStackLayout x:Name="outLyt"    BackgroundColor="Transparent">

        <Border  WidthRequest="1000"     BackgroundColor="White"   StrokeThickness="1"  StrokeShape="RoundRectangle 10,10,10,10"  HorizontalOptions="Center">

            <VerticalStackLayout    >

                <Label  x:Name="lbTitle"   Text="请选择任意一项" FontAttributes="Bold"  HorizontalOptions="Center" Padding="0,15,0,15" FontSize="18"/>

                <Border Stroke="{Static config:ThemeConfig.SubTitle}" BackgroundColor="{Static config:ThemeConfig.SubTitle}"   StrokeThickness="0.2"  />

                <ListView    x:Name="lv"     RowHeight="35"   ItemSelected="lv_ItemSelected"   VerticalScrollBarVisibility="Never">
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <ViewCell>
                                <Grid >
                                    <Grid.RowDefinitions>
                                        <RowDefinition   />
                                    </Grid.RowDefinitions>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition  />
                                    </Grid.ColumnDefinitions>
                                    <Label  Text="{Binding}" FontSize="14"  HorizontalTextAlignment="Center" VerticalTextAlignment="Center" TextColor="Black"/>
                                </Grid>
                            </ViewCell>

                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>

            </VerticalStackLayout>
        </Border>
    </VerticalStackLayout>
</toolkit:Popup>

2.代码

cs 复制代码
public partial class BottomListPopup : CommunityToolkit.Maui.Views.Popup
{
    private Action<int, string> _itemClick;

    public string Title
    {
        set
        {
            lbTitle.Text = value;
        }
    }

    public BottomListPopup(List<string> items, Action<int, string> itemClick, int maxShowCount = 7)
    {
        InitializeComponent();

        if (items == null || items.Count < 2)
        {
            throw new ArgumentException("数据应至少两个!");
        }

        if (items.Distinct().Count()!=items.Count)
        {
            throw new ArgumentException("数据不可存在重复项!");
        }

        lv.ItemsSource = items;

        _itemClick = itemClick;


        if (items.Count > maxShowCount)
        {
            lv.HeightRequest = maxShowCount * 35;
        }
        else
        {
            lv.HeightRequest = items.Count * 35;
        }
    }

    private void lv_ItemSelected(object sender, SelectedItemChangedEventArgs e)
    {
        _itemClick?.Invoke(e.SelectedItemIndex, e.SelectedItem.ToString());

        Close();
    }
}

3.使用

cs 复制代码
BottomListPopup popup = new BottomListPopup(
   new List<string>() { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", }
   , async (index, str) =>
   {
       await Toast.Make($"点击中第{index}项,数据为{str}").Show();
   });

this.ShowPopup(popup);

4.效果

效果

相关推荐
Lost of 程序猿几秒前
命令模式实战:把“下单后的动作“打包成可执行的任务
后端·设计模式·c#·asp.net·命令模式
淡海水14 分钟前
16-01-C#数据结构源码-附录A-术语表
unity·c#·游戏引擎·il2cpp
小羊没烦恼!25 分钟前
【译】利用Asp.net MVC处理文件的上传下载
开发语言·缓存·c#·word·powerpoint·.net
波力海苔夹心脆6751 小时前
C# 海康威视摄像头二次开发入门:HCNetSDK 登录、实时预览、云台控制、录像与抓图(WinForms 实战)
开发语言·windows·经验分享·tcp/ip·c#
淡海水1 小时前
16-03-C#数据结构源码-附录C-参考资源
unity·c#·游戏引擎·il2cpp
asdzx672 小时前
Excel 水印方案对比:页眉图片 vs 背景图片(附 C# 实现)
c#·excel
影寂ldy2 小时前
C# TCP转串口Modbus网关终极完整版笔记(队列防抖+一问一答+帧解析+双客户端轮询)
笔记·tcp/ip·c#
Chen—LSN16 小时前
C语言——文件操作(2)
c语言·开发语言·c++·经验分享·笔记·算法·c#
淡海水19 小时前
00-Unity和C#的GC剖析与对比-全篇导览
unity·c#·游戏引擎·gc
fogota20 小时前
C# `string.Join` 处理 `List<int>`
windows·c#