xamarin表示FlowListView不显示List中的数据

时间:2017-01-03 16:02:01

标签: xamarin.forms

我有一个没有显示列表中项目的FlowListView控件。出于测试目的,我只想将照片的文件路径显示为标签。

这是xaml代码:

<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
        xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
        xmlns:controls="clr-namespace:XLabs.Forms.Controls;assembly=XLabs.Forms.Controls"
        xmlns:flv="clr-namespace:DLToolkit.Forms.Controls;assembly=DLToolkit.Forms.Controls.FlowListView"
        x:Class="TamarianApp.ImagePage">
    <ContentPage.Content>
            <flv:FlowListView FlowColumnCount="3" SeparatorVisibility="None" HasUnevenRows="true" x:Name="image_gallary" ItemsSource="{Binding photos}" HeightRequest="100" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
                <flv:FlowListView.FlowColumnTemplate>
                    <DataTemplate>
                        <Label Text="{Binding filepath}" TextColor="Black" Margin="20" VerticalOptions="Fill" HorizontalOptions="Fill" XAlign="Center" YAlign="Center"></Label>
                    </DataTemplate>
                </flv:FlowListView.FlowColumnTemplate>
            </flv:FlowListView>
    </ContentPage.Content>
</ContentPage>

这是后端:

  public partial class ImagePage : ContentPage
  {
        public List<Photo> photos = App.rug.photos

        public ImagePage()
        {
            InitializeComponent();
            Title = "Photos";
        }
   }

'App.rug.photos'是Photo类的List,其中包含字符串字段'filepath'。调试显示字段'photos'不为空,包含来自'App.rug.photos'的数据。

请帮助。

2 个答案:

答案 0 :(得分:0)

可能与您分配列表的方式有关,您需要确保列表是属性,以便绑定正常工作。

尝试..

public partial class ImagePage : ContentPage
  {
        public List<Photo> photos {get; set;}

        public ImagePage()
        {
            InitializeComponent();
            Title = "Photos";
            photos = App.rug.photos 
        }
   }

您可能还需要设置绑定上下文

public partial class ImagePage : ContentPage
  {
        public List<Photo> photos {get; set;};

        public ImagePage()
        {
            InitializeComponent();
            this.BindingContext = this;
            Title = "Photos";
            photos = App.rug.photos 
        }
   }

答案 1 :(得分:0)

试试这段代码。

public ObservableCollection<object>() photos;
...
var oc = new ObservableCollection<Photo>();
foreach (var item in App.rug.photos)
    oc.Add(item);
photos = oc as ObservableCollection<object>;

我遇到ObservableCollection<MyObject>时遇到问题,但是使用ObservableCollection<object>解决了这个问题。