2016-07-27 45 views
3

我使用StackLayout和ListView來顯示視圖的某些部分,但ListView需要更多的空間比我需要,並留下一個空格之間的列表的最後一行和配置文件的繼續。看來我的ListView具有比實際列表的長度更多的線,或者它有一個固定的高度,我不知道......這是我的XAML:有更多的空間比我在ListView需要

<ScrollView> 
    <StackLayout Orientation="Vertical" Spacing="10"> 

    <Label Text="{Binding Establishment.Name}" TextColor="Black" HorizontalOptions="StartAndExpand" Style="{DynamicResource TitleStyle}" FontAttributes="Bold"/> 

    <Label Text="{Binding Establishment.Category.Name, StringFormat='Categoria: {0}'}" TextColor="Black" HorizontalOptions="StartAndExpand" FontAttributes="Bold"/> 

    <Label Text="{Binding Establishment.Description}" TextColor="Black" HorizontalOptions="StartAndExpand" LineBreakMode="WordWrap" XAlign="Start"/> 

    <Label Text="Contatos" TextColor="Black" HorizontalOptions="StartAndExpand" FontAttributes="Bold"/> 

    <ListView ItemsSource="{Binding Establishment.Contacts}" VerticalOptions="Start" HasUnevenRows="True"> 
     <ListView.ItemTemplate> 
     <DataTemplate> 
      <TextCell Text="{Binding Value}" Detail="{Binding StringType}" DetailColor="Gray"/> 
     </DataTemplate> 
     </ListView.ItemTemplate> 
    </ListView> 

    <Label Text="Endereço" TextColor="Black" HorizontalOptions="StartAndExpand" FontAttributes="Bold"/> 

    <Label Text="{Binding Establishment.FullAddress}" TextColor="Black" HorizontalOptions="StartAndExpand" /> 

    <Button Text="Ver no mapa"/> 

    </StackLayout> 
</ScrollView> 

我怎樣才能解決呢?我看到有人使用HasUnevenRows="True",但它對我沒有用。

+0

你試過在ListView控件設置HeightRequest價值? – Jason

回答

1

謝謝你們,但沒有爲我工作。我認爲我自己的解決方案肯定是不是最好的,但它爲我的作品.....

在XAML我留下了一個空StackLayout這樣的:

<StackLayout x:Name="ContactsList" Orientation="Vertical" Spacing="10" Padding="8,0"> 
     <!-- Empty because it will be filled in code mode --> 
    </StackLayout> 

然後,在cs文件,我打電話後InitializeComponent()方法這個方法:

private void setUpContacts(Establishment establishment) 
    { 
     foreach(Contact contact in establishment.Contacts) 
     { 
      StackLayout row = new StackLayout 
      { 
       Orientation = StackOrientation.Vertical 
      }; 

      row.Children.Add(new Label 
      { 
       TextColor = Color.Gray, 
       Text = string.Format("{0}:", contact.StringType) 
      }); 

      row.Children.Add(new Label 
      { 
       TextColor = Color.Black, 
       FontAttributes = FontAttributes.Bold, 
       Text = contact.Value, 
      }); 

      row.GestureRecognizers.Add(new TapGestureRecognizer { 
       Command = new Command(() => OnContactTapped(row, contact)) 
      }); 

      ContactsList.Children.Add(row); 
     } 
    } 

我做了這一點,因爲名單是不是一個真正的ListView,因爲我並不需要直接在列表滾動功能,它只是另一部分觀點。我認爲這不會導致任何性能問題,因爲它會有少量的項目(最多5個)。我將這個標記爲幫助其他人解決這個問題的正確答案。

但請如果這是一個大NOOB的解決方案,沒有人能做到,回答我,我會找到另一種方式來做到這一點

+1

如果列表中只有一些東西,我認爲你的解決方案是很好,特別是如果它適合你。我在代碼的不同位置用'Grid'做了一些類似的事情。感謝您發佈您如何解決它。 – hvaughan3

+0

感謝您的反饋! –

2

首先,Xamarin建議不要把ListView置於ScrollView之內,因爲他們都提供滾動和互相爭鬥。我親自嘗試過這一點,它通常會引起怪異的行爲,特別是在Android上。

相反,我所做的是使用ListView.HeaderListView.HeaderTemplateListView.FooterListView.FooterTemplate。 Xamarin鏈接here這允許你的確切場景,但沒有奇怪的行爲問題。如果你想看到一個例子,請告訴我。

如果必須必須必須與當前的佈局走了,你可以嘗試看看this後談論如何ListView需要與特定HeightWidthLayoutOptions的大小,因爲它是不應該以適合的尺寸它的內容。在這篇文章中有幾個解決方案,我還沒有嘗試過。

this張貼給出的解決方法之一,說對了ListView添加到StackLayout並添加到另一個StackLayout。然後將內部StackLayoutVerticalOptions設置爲LayoutOptions.FillAndExpand

因此,它可能是這樣的:

<ScrollView> 
    <StackLayout Orientation="Vertical" Spacing="10"> 

    .... 

    <StackLayout VerticalOptions="FillAndExpand"> 
     <ListView ItemsSource="{Binding Establishment.Contacts}" VerticalOptions="Start" HasUnevenRows="True"> 
     <ListView.ItemTemplate> 
      <DataTemplate> 
      <TextCell Text="{Binding Value}" Detail="{Binding StringType}" DetailColor="Gray"/> 
      </DataTemplate> 
     </ListView.ItemTemplate> 
     </ListView> 
    </StackLayout> 

    .... 

    </StackLayout> 
</ScrollView> 
+0

您如何看待我的解決方案? –

2

由於hvaughan3說,這真的不是一個好的做法。但我有同樣的情況,我用的解決方法與行爲:

public class ListViewHeightBehavior : Behavior<ListView> 
{ 
    private ListView _listView; 

    public static readonly BindableProperty ExtraSpaceProperty = 
     BindableProperty.Create(nameof(ExtraSpace), 
           typeof(double), 
           typeof(ListViewHeightBehavior), 
           0d); 

    public double ExtraSpace 
    { 
     get { return (double)GetValue(ExtraSpaceProperty); } 
     set { SetValue(ExtraSpaceProperty, value); } 
    } 

    protected override void OnAttachedTo(ListView bindable) 
    { 
     base.OnAttachedTo(bindable); 

     _listView = bindable; 
     _listView.PropertyChanged += (s, args) => 
     { 
      var count = _listView.ItemsSource?.Count(); 
      if (args.PropertyName == nameof(_listView.ItemsSource) 
        && count.HasValue 
        && count.Value > 0) 
      { 
       _listView.HeightRequest = _listView.RowHeight * count.Value + ExtraSpace; 
      } 
     }; 
    } 
} 

XAML:

 <ListView ItemsSource="{Binding Items}" 
       HasUnevenRows="True"> 
     <ListView.Behaviors> 
     <behaviors:ListViewHeightBehavior ExtraSpace="180"/> 
     </ListView.Behaviors> 
     <ListView.ItemTemplate> 
     <DataTemplate> 
      <ViewCell> 
      // Your template 
      </ViewCell> 
     </DataTemplate> 
     </ListView.ItemTemplate> 
    </ListView> 
+0

您對我的解決方案有何看法? –

+0

如果只有5件商品,那麼我希望這不是什麼大問題。但我認爲,我的行爲正在我身邊工作,你在實施中錯過了一些東西。 –

+0

是的可以。當我調試你的解決方案時,它對我很有幫助,但我不知道爲什麼RowHeight有時以-1爲單位:/ –

相關問題