2017-05-08 15 views
1

私はリストビューをデフォルトのセパレータビジビリティで持っています。私のAndroidプロジェクトは、ItemsSourceに要素がある場合にSeparatorを表示し、最後の要素の下にその要素を表示しなくなります。それが私のiOSプロジェクトにとって欲しい結果です。Xamarin.Forms(iOSプロジェクト):リストビューセパレータがすべての画面に表示されます

しかし、私のiOSプロジェクトでは、何も要素がない場合でも、1つしかない場合でも、区切り記号はまだ存在します。

誰かが私に理由を教えてくれますか、どうしたらいいですか?ありがとう。

答えて

2

私はあなたがデフォルトの区切りを無効にし、これらはいくつかのヒントです

まずthis post

に見てみることができ、これが後のListView XAML

SeparatorColor="Transparent" 

に次のプロパティを追加することによって行われていると思いますこれは、ダブルStackLayoutの中に完全なViewCellコンテンツをラップします!私はこれが過度のように聞こえるが、この方法では、ViewCell ...または他のものの内部の余白に関するBoxViewの問題に遭遇することはないだろう。 最初のStackLayoutは、区切り記号を設定する色にBackgroundColorを設定する必要があります.2番目のStackLayoutは、この例のページでは、残りのコンテナと同じBackgroundColorを持つ必要があります。この2番目のStackLayoutの下に余白を追加するのは、セパレータの太さを表すためです。

は、私はあなたがこの「証拠金」...あなたのデータが空になったときに、代わりにそれとセパレーター

<ListView x:Name="SeparatorListView" 
    SeparatorColor="Transparent" 
    ItemsSource="{Binding Persons}" 
    Margin="0,20,0,0" 
    RowHeight="60" 
    HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" 
    BackgroundColor="White" 
    Grid.Row="1"> 
    <ListView.ItemTemplate> 
     <DataTemplate> 
      <ViewCell IsEnabled="false"> 
       <StackLayout VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" 
        BackgroundColor="Black"> 
       <StackLayout VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" 
        BackgroundColor="White" 
        Margin="0,0,0,0.4"> 
        <StackLayout VerticalOptions="CenterAndExpand" HorizontalOptions="FillAndExpand" Spacing="0"> 
         <Label Text="{Binding FullName}" TextColor="Maroon" VerticalOptions="CenterAndExpand" Margin="20,0,20,0" /> 
         <Label Text="{Binding Profession}" TextColor="Maroon" VerticalOptions="CenterAndExpand" Margin="20,0,20,0" /> 
        </StackLayout> 
       </StackLayout> 
       </StackLayout> 
      </ViewCell> 
     </DataTemplate> 
    </ListView.ItemTemplate> 
</ListView> 

を持つべきではありませんので、余裕を削除すると遊ぶことができると思い、あなたが買ってあげますこのブログ記事の右上にあるプレビュー画像と同じ視覚的な結果です。

ボーナスとして、あなたのページに白以外の背景色がある場合は、StackLayoutsの1つを省略することができます。これが当てはまる場合、ListViewの内部で透明で再生することで、その色をセパレータの色として使用できます。

この例では、ページ自体にもOlive!に設定されたBackgroundColorがある場合にのみ、ノートが機能します。

<ListView x:Name="SeparatorListView" 
    SeparatorColor="Transparent" 
    ItemsSource="{Binding Persons}" 
    Margin="0,20,0,0" 
    RowHeight="60" 
    HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" 
    BackgroundColor="Olive" 
    Grid.Row="1"> 
    <ListView.ItemTemplate> 
     <DataTemplate> 
      <ViewCell IsEnabled="false"> 
        <StackLayout BackgroundColor="#f4eac3" 
           Padding="0,5,0,5" 
           HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand"> 
         <StackLayout BackgroundColor="Transparent" 
            HorizontalOptions="FillAndExpand" VerticalOptions="CenterAndExpand" 
            Spacing="0" 
            Margin="20,0,20,0"> 
         <Label Text="{Binding FullName}" TextColor="Maroon" VerticalOptions="CenterAndExpand" /> 
         <Label Text="{Binding Profession}" TextColor="Maroon" VerticalOptions="CenterAndExpand" /> 
        </StackLayout> 
       </StackLayout> 
      </ViewCell> 
     </DataTemplate> 
    </ListView.ItemTemplate> 
</ListView> 
+0

おかげ@AlessandroCaliaro!私のページのBackgroundImageを持っていて、ListViewに 'BackgroundColor =" Transparent "があるので、私はうまくいきませんでした。そのトリックはできませんでした。 私のSeparatorVisibility = "None"を作成し、私のSeparatorをシミュレートしたBackgroundColorを使ってグリッドに追加する(私は各ViewCellの中に1つずつ)それは完璧に動作します、ありがとう! –

0

これは、iOS側のListViewの実装によるものです。これは、の場合、常に高さ全体を取り、空のアイテムを表示するUITableViewとしてレンダリングされます。このタイプの動作を変更するには、ページのコードビハインドに動的にListViewの高さを設定します。 XamarinのEvolve sample appには、CardViewListViewを組み合わせた数ページで、Androidの場合と同じように表示されます。

1
using UIKit; 
using Xamarin.Forms; 
using Xamarin.Forms.Platform.iOS; 

[assembly: ExportRenderer (typeof(ListView), typeof(CustomListViewRenderer))] 
namespace yourNamespace 
{ 
    public class CustomListViewRenderer : ListViewRenderer 
    { 
     protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.ListView> e) 
     { 
      base.OnElementChanged(e); 
      if (e.NewElement != null) 
      { 
       var listView = Control as UITableView; 
       listView.SeparatorInset = UIEdgeInsets.Zero; 
      } 
     } 
    } 
} 
関連する問題