简体   繁体   English

Xamarin表单中的ListView行为中缺少某些内容

[英]Missing something in Behavior of ListView in Xamarin Forms

I don't know what I am missing in 4th line. 我不知道我在第4行中缺少什么。 I have ListView in page and I want to set behavior whenever size change. 我在页面中有ListView,并且我想在大小更改时设置行为。

public class ListViewAutoSizeBehavior : Behavior<ListView>
    {
        ListView _ListView;
        //Error here. ITemplatedItemsView<TItem>
        ITemplatedItemsView Cells => _ListView;

        private readonly int _extraPaddingPerRow;

        public ListViewAutoSizeBehavior()
        {
            switch (Device.RuntimePlatform)
            {
                default:
                    _extraPaddingPerRow = 2;
                    break;
                case Device.Android:
                    _extraPaddingPerRow = 6;
                    break;
                case Device.iOS:
                    _extraPaddingPerRow = 4;
                    break;
                case Device.WinPhone:
                    _extraPaddingPerRow = 2;
                    break;
            }
        }

        protected override void OnAttachedTo(ListView bindable)
        {
            bindable.ItemAppearing += AppearanceChanged;
            bindable.ItemDisappearing += AppearanceChanged;
            _ListView = bindable;
        }

        protected override void OnDetachingFrom(ListView bindable)
        {
            bindable.ItemAppearing -= AppearanceChanged;
            bindable.ItemDisappearing -= AppearanceChanged;
            _ListView = null;
        }

        private void AppearanceChanged(object sender, ItemVisibilityEventArgs e)
        {
            UpdateHeight(e.Item);
        }

        private void UpdateHeight(object item)
        {
            if (_ListView == null || _ListView.ItemsSource == null) return;

            int itemsCount = _ListView.ItemsSource.Cast<object>().Count();

            if (_ListView.HasUnevenRows)
            {
                double height;
                if ((height = _ListView.HeightRequest) == (double)VisualElement.HeightRequestProperty.DefaultValue)
                {
                    height = 0;
                }

                height += MeasureRowHeight(item);
                SetHeight((height + _extraPaddingPerRow) * itemsCount + _extraPaddingPerRow);
            }
            else if (_ListView.RowHeight == (int)ListView.RowHeightProperty.DefaultValue)
            {
                var height = MeasureRowHeight(item);
                _ListView.RowHeight = (height + _extraPaddingPerRow);
                SetHeight((height + _extraPaddingPerRow) * itemsCount + _extraPaddingPerRow);
            }
        }

        private int MeasureRowHeight(object item)
        {
            var template = _ListView.ItemTemplate;
            var cell = (Cell)template.CreateContent();
            cell.BindingContext = item;

            var height = cell.RenderHeight;
            var mod = height % 1;

            if (mod > 0)
            {
                height = height - mod + 1;
            }

            return (int)height;
        }

        private void SetHeight(double height)
        {
            //TODO if header or footer is string etc.
            if (_ListView.Header is VisualElement header)
            {
                height += header.Height;
            }

            if (_ListView.Footer is VisualElement footer)
            {
                height += footer.Height;
            }

            _ListView.HeightRequest = height;
        }
    }

Can anybody please suggest me what I am missing here? 有人可以建议我这里我所缺少的吗? It looks like I need to set some class name? 看起来我需要设置一些类名吗? or name of some control. 或某些控件的名称。

Please suggest. 请提出建议。

Your problem is most likely due to naming conventions. 您的问题很可能是由于命名约定引起的。

Listview is a reserved keyword, and _Listview must be confusing the compiler. Listview是保留关键字,并且_Listview必须使编译器感到困惑。

Try to rename that variable, and refactor your code, so there won't be any issue. 尝试重命名该变量,然后重构代码,这样就不会有任何问题。

 ListView _myListView;
 ITemplatedItemsView Cells => _myListView;

You didn't put any error, so I'm assuming this is the main cause. 您没有输入任何错误,所以我认为这是主要原因。 If not, please submit the error message for further analysis. 如果不是,请提交错误消息以进行进一步分析。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM