5.列表选择弹窗(BottomListPopup)

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

环境:.NET 7、MAUI 

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

1.布局

<?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.代码 


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.使用

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.效果

效果

  • 8
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
你可以使用layer.open函数来打开显示列表。根据你提供的引用内容,以下是一个示例代码: ```javascript layer.open({ type: 2, title: "查看详情", shadeClose: false, shade: 0.5, btn: \['关闭'\], maxmin: false, area: \['80%', '80%'\], content: "open.html?id=" + id, cancel: function(){}, end: function(){} }); ``` 在这个示例中,type参数设置为2表示打开一个iframe层,content参数指定了要显示的内容,可以是一个URL或者HTML代码。你可以根据自己的需求修改这个示例代码来显示你的列表内容。 #### 引用[.reference_title] - *1* [layer.open框打开html内容并调用接口显示数据](https://blog.csdn.net/weixin_43991257/article/details/127551805)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [layer open 打开口用法](https://blog.csdn.net/wobengkui/article/details/128469337)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [layer.open位置修改](https://blog.csdn.net/weixin_57839268/article/details/123473030)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

就是那个帕吉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值