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

Dynamically hiding Xamarin Forms ListView item and setting the height of invisible item to 0 not working on iOS

在 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.
                }
            }
        };
    }
}

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

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

public class ForceUpdateSizeTriggerAction : TriggerAction<VisualElement>
{
    public double 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="{Binding xxx}"  Value="False">  /// xxx here is the property which binding to the IsVisible of stacklayout
                   <DataTrigger.EnterActions>
                        <local:ForceUpdateSizeTriggerAction HeighRequest="0.01" 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"