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

需要Xamarin CollectionView行为才能再次运行

如何解决需要Xamarin CollectionView行为才能再次运行

我创建了一个行为,该行为可以调整collectionview行的大小,以防止collectionview调用其自身的滚动行为,并防止最后一行之后的空间过多。

XAML

<CollectionView ItemsSource="{Binding}" Margin="-55,0">

    <CollectionView.ItemsLayout>
        <LinearItemsLayout Orientation="Vertical" ItemSpacing="0"/>
    </CollectionView.ItemsLayout>

    <CollectionView.Behaviors>
        <core:CollectionViewRectangLeverticleHeightBehavior/>
    </CollectionView.Behaviors>

    <CollectionView.ItemTemplate>
        <DataTemplate>
            ....
        </DataTemplate>
    </CollectionView.ItemTemplate>
</CollectionView>

文件(后面没有代码

public class CollectionViewRectangLeverticleHeightBehavior : Behavior<CollectionView>
{
    bool hasHeightChanged = false;

    public CollectionView CollectionViewObject { get; private set; }

    protected override void OnAttachedTo(CollectionView bindable)
    {
        base.OnAttachedTo(bindable);
        CollectionViewObject = bindable;
        bindable.BindingContextChanged += Bindable_BindingContextChanged;
        bindable.SizeChanged += Bindable_SizeChanged;
    }

    ...

    private void Bindable_SizeChanged(object sender,EventArgs e)
    {
        if (!hasHeightChanged)
        {
            if (CollectionViewObject.BindingContext is Grouping<string,Estimate> grp)
            {
                CollectionViewObject.HeightRequest = (grp.Count * (32 + grp.Count == 1 ? 0 : 66 + 50));
                hasHeightChanged = true;
            }
        }
    }

    ...
}

在提供给ItemsSource的初始数据下,它可以正常工作。但是,当我将记录添加到视图模型中的ObservableCollection时,该行为不会再次运行,从而导致出现滚动条。

如何强制CollectionView自行“重绘”,以便再次运行“行为”?理想情况下,如果滚动条即将出现并公开一个方法调用当前行的行为,则将触发事件。我在现实中找不到类似的东西。

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