【问题标题】:Xamarin ListView update cell displayXamarin ListView 更新单元格显示
【发布时间】:2018-04-18 08:05:25
【问题描述】:

我使用滑块更改单元格的大小,但 listView 的单元格没有更新 怎么做?还缺少什么?

添加新项目也不会显示,但如果你用调试器检查它们,列表中有这样的项目

private void Slider_ValueChanged(object sender, ValueChangedEventArgs e)
{
    lstView.RowHeight = (int)e.NewValue;
}

本作品的完整代码:

public class RootPage : ContentPage
    {
        ListView listView;
        public RootPage ()
        {
            listView = new ListView() { HorizontalOptions = LayoutOptions.StartAndExpand };
            listView.ItemTemplate = new DataTemplate(typeof(MyCustomCell));
            listView.ItemsSource = new CustomText[] { new CustomText("asd", "bsd"), new CustomText("abra", "kadabra") };
            listView.HasUnevenRows = true;
            var slider = new Slider() {
                VerticalOptions = LayoutOptions.EndAndExpand,
                Maximum = 300,
                Minimum = 100
            };
            slider.ValueChanged += Slider_ValueChanged;
            Content = new StackLayout {
                Spacing = 20,
                Children = {
                    listView,
                    slider
                }
            };
        }

        private void Slider_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            listView.RowHeight = (int)e.NewValue;
        }
    }



public class CustomText
    {
        public string T1 { get; set; }
        public string T2 { get; set; }
        public CustomText(string t1, string t2)
        {
            T1 = t1;
            T2 = t2;
        }
    }
    public class MyCustomCell : ViewCell
    {
        public MyCustomCell()
        {
            var label1 = new Label();
            var label2 = new Label();

            label1.SetBinding(Label.TextProperty, new Binding("T1"));
            label2.SetBinding(Label.TextProperty, new Binding("T2"));

            var horizontalLayout = new StackLayout()
            {
                BackgroundColor = Color.Olive,
                Children = {
                    label1,
                    label2
                }
            };

            View = horizontalLayout;
        }
    }

【问题讨论】:

  • 尝试更改主线程上的RowHeightDevice.BeginInvokeOnMainThread(() => { code here });
  • @DennisSchröer 我试过Device.BeginInvokeOnMainThread(() => { listView.RowHeight = (int)e.NewValue; });,它不起作用

标签: c# listview xamarin cell


【解决方案1】:

这一行:

            listView.HasUnevenRows = true;

确切地说,更改列表视图的 RowHeight 没有效果,因为高度是为每一行定义的,而不是列表视图。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-04
    • 2023-03-19
    • 2015-04-07
    • 2019-09-02
    • 2015-02-02
    • 1970-01-01
    • 1970-01-01
    • 2015-02-27
    相关资源
    最近更新 更多