为了账号安全,请及时绑定邮箱和手机立即绑定

Xamarin iOS Custom ListView

Xamarin iOS Custom ListView

C#
莫回无 2022-08-20 17:05:54
我在 .我希望 所包含的物品只占用尽可能多的空间。但是,我几乎总是占用所有空间,即使它只包含3个左右的项目。ListViewStackedLayoutListViewListView有没有办法创建自定义并覆盖某些方法,以便在添加或删除项目时计算自定义大小?例如,我希望列表在为空时消失。任何人都可以告诉我,要重写哪些方法和/或如何对添加到/删除到基础模型的项目做出反应?DynamicListView我的直觉告诉我这样的事情:namespace Foo.Controls{    public class SpottedView : ListView    {        public SpottedView()        {        }        protected override void OnChildAdded(Element child)        {            Console.WriteLine($"{Util.methodName()}");            base.OnChildAdded(child);            InvalidateMeasure();        }        protected override void OnChildRemoved(Element child)        {            Console.WriteLine($"{Util.methodName()}");            base.OnChildRemoved(child);            InvalidateMeasure();        }    }}
查看完整描述

1 回答

?
慕莱坞森

TA贡献1810条经验 获得超4个赞

我找到了解决这个问题的方法。我从 更改为 以下内容:StackLayoutGrid


<ContentPage.Content>

    <Grid>

        <Grid.RowDefinitions>

            <RowDefinition Height="*"/>

            <RowDefinition Height="Auto" />

        </Grid.RowDefinitions>


        <ctrl:CameraView Grid.Row="0" />

        <ctrl:SpottedView Grid.Row="1" RowHeight="40" />


    </Grid>

</ContentPage.Content>

然后我添加了一个 自定义 ,如下所示:ListViewSpottedView


namespace Foo.Controls

{

    public class SpottedView : ListView

    {

        ObservableCollection<string> items;


        public SpottedView() : base()

        {

            items = new ObservableCollection<string>();

            ItemsSource = items;

            items.CollectionChanged += onItemsChanged;


            // TODO: test purposes, remove later

            var autoEvent = new AutoResetEvent(false);

            var stateTimer = new Timer(onTimer, autoEvent, 1000, 2000);

        }


        void onItemsChanged(object sender, NotifyCollectionChangedEventArgs e)

        {

            MainThread.BeginInvokeOnMainThread(() =>

            {

                HeightRequest = RowHeight * items.Count;

                InvalidateMeasure();

            });

        }


        // TODO test purposes, remove later.

        void onTimer(object state)

        {

            MainThread.BeginInvokeOnMainThread(() =>

            {

                if (items.Count < 2)

                {

                    items.Add("Blah");

                }

                else

                {

                    items.Clear();

                }

            });

        }

    }

}

像吊饰一样工作。


查看完整回答
反对 回复 2022-08-20
  • 1 回答
  • 0 关注
  • 104 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号