动态显示/隐藏Xamarin.Forms.ListView的页眉或页脚

时间:2017-01-27 13:28:27

标签: listview xamarin xamarin.forms

有没有办法根据运行时的条件动态显示/隐藏ListView的标题。

<ListView x:Name="ListViewChallenges" Header="{Binding .}">

    <ListView.FooterTemplate>
        <DataTemplate>
            <Label Text="No Elements found." IsVisible="{Binding FooterIsVisible}" />
        </DataTemplate>
    </ListView.FooterTemplate>

    <!-- ... -->

</ListView>

在BindingContext中,我声明了FooterIsVisible属性。当它是假的时,页脚应该是不可见的。但是这不起作用,页脚总是占用ListView底部的Label的一定空间。

这有可能吗?

7 个答案:

答案 0 :(得分:6)

您应该能够隐藏页脚,而不是占用任何空间。我相信您需要为<ion-slide-page ng-repeat="item in lista_tutorial[0].lista_slide" ng-if="!$first"> <div ng-if="item.tipo == 'benvenuto'" class="divBenvenutoTreVeline"> ABC </div> <div ng-if="item.tipo == 'home'" class="divBenvenutoTreVeline"> BCD </div> <ion-slide-page> 中的标签设置HeightRequest。你可以通过以下方式做到这一点:

FooterTemplate

答案 1 :(得分:1)

您应该使用FooterTemplate和Footer属性。它作为ItemTemplate和ItemSource:

<ListView Footer="{Binding IsFooterVisible}">
    <ListView.FooterTemplate>
        <DataTemplate>
            <!-- Footer content. Always visible -->
        </DataTemplate>
    <ListView.FooterTemplate>
</ListView>

并绑定到Footer属性的东西可以为空(例如对象)。或者您可以使用转换器转换:true - &gt; new object()和false - &gt;空

还可以创建ListView的子类。我的例子(IsLoading属性是你要搜索的):

的Xaml:

<?xml version="1.0" encoding="UTF-8"?>
<ListView xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    x:Class="Panor.Views.LoadableListView"
    x:Name="element"
    >
    <ListView.FooterTemplate>
        <DataTemplate>
            <ContentView>
                <ActivityIndicator IsRunning="true"
                    Margin="0, 5"
                    Color="{StaticResource mainColor}"
                    VerticalOptions="CenterAndExpand"
                    HorizontalOptions="CenterAndExpand"
                    >
                    <ActivityIndicator.Scale>
                        <OnPlatform x:TypeArguments="x:Double" iOS="2" Android="1" />
                    </ActivityIndicator.Scale>
                </ActivityIndicator>
            </ContentView>            
        </DataTemplate>
    </ListView.FooterTemplate>
</ListView>

代码隐藏:

public partial class LoadableListView : ListView
{
    public LoadableListView()
    {
        InitializeComponent();

        this.ItemAppearing += OnItemAppearing;
    }

    public static readonly BindableProperty IsLoadingProperty = BindableProperty.Create(
        nameof(IsLoading),
        typeof(bool),
        typeof(LoadableListView),
        false,
        propertyChanged: (bindable, oldValue, newValue) => 
        {
            var element = (LoadableListView)bindable;

            element.Footer = (bool)newValue ? new object() : null;
        }
    );
    public bool IsLoading
    {
        set => SetValue(IsLoadingProperty, value);
        get => (bool)GetValue(IsLoadingProperty);
    }

    public static readonly BindableProperty ScrolledDownCommandProperty = BindableProperty.Create(
        nameof(ScrolledDownCommand),
        typeof(ICommand),
        typeof(LoadableListView)
    );
    public ICommand ScrolledDownCommand
    {
        set => SetValue(ScrolledDownCommandProperty, value);
        get => (ICommand)GetValue(ScrolledDownCommandProperty);
    }

    void OnItemAppearing(object sender, ItemVisibilityEventArgs e)
    {
        if (ItemsSource == null) return;
        if (ScrolledDownCommand == null) return;

        object last = null;

        if (ItemsSource is IList)
        {
            var length = (ItemsSource as IList).Count;
            last = (ItemsSource as IList)[length - 1];
        }
        else 
        {
            foreach (var item in ItemsSource)
                last = item;
        }

        if (e.Item == last && ScrolledDownCommand.CanExecute(null))
            ScrolledDownCommand.Execute(null);

    }

耗竭与:

 <views:LoadableListView ItemsSource="{Binding ItemSource}"
                RowHeight="120"
                SeparatorColor="#c7c8c9" 
                IsLoading="{Binding IsMoreLoading}"
                ScrolledDownCommand="{Binding ScrolledDownCommand}"
                IsPullToRefreshEnabled="true"
                IsRefreshing="{Binding IsRefreshing}"
                RefreshCommand="{Binding RefreshCommand}"
                >
                <views:LoadableListView.ItemTemplate>
                    <DataTemplate>
                        <cells:MagazinesListCell Name="{Binding Name}" 
                            Publisher="{Binding Publisher}"
                            Price="{Binding Price}"
                            Image="{Binding Converter={StaticResource UriToImageSourceConvertor}, Path=Image}"
                            Commands="{Binding Commands}"
                            />
                    </DataTemplate>
                </views:LoadableListView.ItemTemplate>
            </views:LoadableListView>

答案 2 :(得分:0)

您可以将页脚模板包裹在ViewCell中,然后在您不希望它显示时将ViewCell的{​​{1}}设置为0。如果您这样做,请务必将Height属性设置为ListView.HasUnevenRows

True

答案 3 :(得分:0)

以下是解决方案:

&#13;
&#13;
<ListView.FooterTemplate>
  <DataTemplate>

    <!--VIEW-->
    <ActivityIndicator IsRunning="true" IsVisible="{Binding IsLoadingOnDemand}" HorizontalOptions="CenterAndExpand" Margin="15">
      <ActivityIndicator.Triggers>
        <Trigger TargetType="ActivityIndicator" Property="IsVisible" Value="False">
          <Setter Property="HeightRequest" Value="0" />
        </Trigger>
      </ActivityIndicator.Triggers>
    </ActivityIndicator>

  </DataTemplate>
</ListView.FooterTemplate>
&#13;
&#13;
&#13;

答案 4 :(得分:0)

这对我来说没有temlpate(如果您需要从父.cs代码中的标题引用元素,则模板将不起作用)。因此,必须将容器的高度设为0而不是0,并切换可见性。

.SendMail

答案 5 :(得分:0)

如果由于列表视图为空而要隐藏页脚,只需设置

semaphore_t mSemaphore;

kern_return_t result = semaphore_create(mach_task_self(), &mSemaphore, SYNC_POLICY_FIFO, 0);

// Do stuff with semaphore wait and signal ...

kern_return_t result = semaphore_destroy(mach_task_self(), mSemaphore);

答案 6 :(得分:0)

我尝试了以下所有变体:
1)Header属性绑定new object()null
2)两个没有转换器(https://stackoverflow.com/a/53197844/7429947)的堆栈布局
3)DataTriggerTrigger在变更IsVisible上设置为HeightRequest

这对我没有帮助,然后我在“ HeightRequest”上替换了MinimumHeightRequest,第二个变体对iOS和Android有所帮助!

Xamarin.Forms版本:4.6.0.726

相关问题