动态隐藏Xamarin Forms ListView项,并将不可见项的高度设置为0,在iOS上不起作用

问题描述 投票:0回答:1

在Xamarin Forms ListView中,将StackLayout放置在ViewCell中。下面的触发器用于将StackLayout的高度和边距设置为0,以便ListView中没有间隙。

<StackLayout.Triggers>
    <Trigger TargetType="StackLayout" Property="IsVisible" Value="False">
        <Setter Property="HeightRequest" Value="0" />
        <Setter Property="Margin" Value="0" />
    </Trigger>
</StackLayout.Triggers>

该代码在Android上运行完美。但是在iOS上仍然存在差距。

通常在敲击事件上调用ViewCell.ForceUpdateSize可以帮助解决此类问题。但是我们需要在语法上做到这一点。因此,我尝试创建一个CustomViewCell,但这没有帮助。

public class CustomViewCell : ViewCell
{
    protected override void OnBindingContextChanged()
    {
        base.OnBindingContextChanged();

        var viewModel = (tecommobile.ViewModels.Input)BindingContext;
        viewModel.PropertyChanged += (sender, e) =>
        {
            if (Device.RuntimePlatform == Device.iOS)
            {
                if (e.PropertyName == "IsVisible")
                {
                    //ForceUpdateSize(); crashes, the possible cause could be the height of the StackLayout is already set to 0. The test shows that the tapped event doesn't fire if the height is 0.
                }
            }
        };
    }
}

请提供解决方案或任何解决方法。谢谢。

xamarin.forms listviewitem
1个回答
0
投票

我们应该使用TriggerAction更改此值,并告诉ViewCell更新其大小。

public class ForceUpdateSizeTriggerAction : TriggerAction<VisualElement>
{
    public int HeighRequest { set; get; }  // you could set it as bindable property if you want to binding its value in runtime

    public Thickness CustomMargin { set; get; }

    public ForceUpdateSizeTriggerAction() : base()
    {

    }
    protected override void Invoke(VisualElement sender)
    {

        var parent = sender.Parent;
        while (parent != null && !(parent is ViewCell))
        {
            parent = parent.Parent;
        }
        if (parent is ViewCell cell)
        {
            Device.BeginInvokeOnMainThread(() =>
            {
                var view = sender as View;
                view.HeightRequest = HeighRequest;
                view.Margin = CustomMargin;
                cell.ForceUpdateSize();
            });
        }
    }
}

在xaml中

<StackLayout.Style>
   <Style TargetType="StackLayout">
       <Setter Property="HeightRequest" Value="xxx"/> //default height
          <Style.Triggers>
            <DataTrigger TargetType="StackLayout" Binding="IsVisible" Value="False">
                   <DataTrigger.EnterActions>
                        <local:ForceUpdateSizeTriggerAction HeighRequest="0" CustomMargin="0"/>
                   </DataTrigger.EnterActions>

                   <DataTrigger.ExitActions>
                        <local:ForceUpdateSizeTriggerAction HeighRequest="xxx" CustomMargin="xxx"/> // set the default value of height and margin here
                   </DataTrigger.ExitActions>
          </DataTrigger>
       </Style.Triggers>
   </Style>
</StackLayout.Style>

并且不要忘记设置列表视图的HasUnevenRows="True"。>>

© www.soinside.com 2019 - 2024. All rights reserved.