微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

从后面的代码输入每个键盘后调整 ListView 的大小在 stacklayout 内

如何解决从后面的代码输入每个键盘后调整 ListView 的大小在 stacklayout 内

-> xamarin 表单在每次键盘输入后调整列表视图的大小(在 stacklayout 内)-来自后面的代码

(如果可能的话,想要这个跨平台的吗?)

使用 SearchBar 允许用户搜索葡萄酒产品...当文本更改时,显示的结果会正确更新,但我想更改 ListView 的大小以仅显示结果删除任何空白区域。

例如,在附加的图片条目中,'Wine' 返回 12 个结果,它显示了所有......但'Wine 1'只有 4 个结果。所以我希望 ListView 在 'wine 12' 之后结束(来自结果),而不是不透明度覆盖屏幕的其余部分。

已经尝试过一些示例,例如: ListView inside StackLayout: How to auto resize the ListView?

但仍然无法进行,有没有人看到我做错了什么,谢谢

    <NavigationPage.TitleView>
            <StackLayout HorizontalOptions="StartAndExpand" Orientation="Horizontal">
                <SearchBar x:Name="SearchBar" TextChanged="SearchBar_TextChanged" HorizontalOptions="FillAndExpand" Placeholder="Search..." PlaceholderColor="Gray" TextColor="White" VerticalOptions="StartAndExpand"/>
            </StackLayout>
        </NavigationPage.TitleView>
     private void SearchBar_TextChanged(object sender1,TextChangedEventArgs e1)
            {
                StackSearchResults.IsVisible = true;
                int numberOfProducts = 0;
                SearchListView.ItemsSource = GetProducts(out numberOfProducts,e1.NewTextValue);
    
                SearchListView.RowHeight = 50;
    
                SearchListView.PropertyChanged += (object sender,System.ComponentModel.PropertyChangedEventArgs e) =>
                {
                    if (e.PropertyName == "ItemsSource")
                    {
                        try
                        {
                            if (SearchListView.ItemsSource != null)
                            {
                                SearchListView.HeightRequest = numberOfProducts * 50;
                            }
                        }
                        catch (Exception ex)
                        {
    
                        }
                    }
                };
    }

enter image description here

解决方法

您只需要分配 PropertyChanged 处理程序一次,而不是每次 TextChanged 触发时。这可能不是根本问题,但无济于事。您还应该确保在 UI 线程上分配 HeightRequest

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。