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

Xamarin.forms 动态隐藏Xamarin Forms ListView项并将不可见项的高度设置为0不在iOS上工作,xamarin.forms,listviewitem,Xamarin.forms,Listviewitem,在Xamarin表单列表视图中,StackLayout放置在ViewCell中。以下触发器用于将StackLayout的高度和边距设置为0,以便ListView中没有间隙 <StackLayout.Triggers> <Trigger TargetType="StackLayout" Property="IsVisible" Value="False"> <Setter Property="HeightRequest" Value="0" /

在Xamarin表单列表视图中,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>

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

我们应该使用触发操作来更改此值,并告诉查看单元格更新其大小

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();
            });
        }
    }
}
公共类ForceUpdateSizeTriggerAction:TriggerAction
{
public double HeighRequest{set;get;}//如果希望在运行时绑定其值,可以将其设置为可绑定属性
公共页边距{set;get;}
public ForceUpdateSizeTriggerAction():base()
{
}
受保护的覆盖无效调用(VisualElement发送方)
{
var parent=sender.parent;
while(parent!=null&!(parent是ViewCell))
{
父=父。父;
}
如果(父项为ViewCell)
{
Device.beginInvokeMainThread(()=>
{
var view=发送方作为视图;
view.HeightRequest=HeightRequest;
view.Margin=CustomMargin;
cell.ForceUpdateSize();
});
}
}
}
在xaml中

//默认高度
///这里是绑定到stacklayout的IsVisible的属性
//在此处设置高度和边距的默认值

不要忘记设置listview的
hasRows=“True”

嗨,卢卡斯,谢谢你的代码。找不到“Binding”的属性、可绑定属性或事件,或者值和属性之间的类型不匹配。在Binding=“IsVisible”中检查我的更新答案,将xxx(IsVisible)绑定到触发器。我更改为Binding=“{Binding Source={x:Reference viewCellStackLayout},Path=IsVisible}”。然后进行编译。这是正确的吗?发布您的完整代码,或者您最好共享您的示例。这里有一个类似的问题,您可以检查一下。
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();
            });
        }
    }
}
<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>