FlowListView Xamarin.Forms的问题

时间:2016-10-27 12:21:32

标签: xamarin xamarin.forms

我正在使用FlowListView在我的xamarin表单应用程序中设置图库视图,使用以下代码..

 public class Page1 : ContentPage
    {

        public Page1()
        {
            ObservableCollection<ItemModel> List = new ObservableCollection<ItemModel>();
            string[] images = {
                "https://farm9.staticflickr.com/8625/15806486058_7005d77438.jpg",
                "https://farm5.staticflickr.com/4011/4308181244_5ac3f8239b.jpg",
                "https://farm8.staticflickr.com/7423/8729135907_79599de8d8.jpg",
                "https://farm3.staticflickr.com/2475/4058009019_ecf305f546.jpg",
                "https://farm6.staticflickr.com/5117/14045101350_113edbe20b.jpg",
                "https://farm2.staticflickr.com/1227/1116750115_b66dc3830e.jpg",
                "https://farm8.staticflickr.com/7351/16355627795_204bf423e9.jpg",
                "https://farm1.staticflickr.com/44/117598011_250aa8ffb1.jpg",
                "https://farm8.staticflickr.com/7524/15620725287_3357e9db03.jpg",
                "https://farm9.staticflickr.com/8351/8299022203_de0cb894b0.jpg",
            };

            int number = 0;
            for (int n = 0; n < 20; n++)
            {
                for (int i = 0; i < images.Length; i++)
                {
                    number++;
                    var item = new ItemModel()
                    {
                        ImageUrl = images[i],
                        FileName = string.Format("image_{0}.jpg", number),
                    };

                    List.Add(item);
                }
            }

            FlowListView listView = new FlowListView()
            {
                FlowColumnTemplate = new DataTemplate(typeof(ListCell)),
                SeparatorVisibility = SeparatorVisibility.None,
                HasUnevenRows = true,
                FlowColumnMinWidth = 110,
                FlowItemsSource = List,
            };
            listView.FlowItemTapped += (s, e) =>
            {
                var item = (ItemModel)e.Item;
                if (item != null)
                {
                    App.Current.MainPage.DisplayAlert("Alert", "Tapped {0} =" + item.FileName, "Cancel");
                }
            };

            Content = new StackLayout
            {
                Children = {
                   listView
                }
            };
        }
    }
    public class ItemModel
    {
        public string ImageUrl { get; set; }
        public string FileName { get; set; }

    }
    public class ListCell : View
    {
        public ListCell()
        {
            CachedImage IconImage = new CachedImage
            {
                HeightRequest = 100,
                Aspect = Aspect.Fill,
                DownsampleHeight = 100,
                DownsampleUseDipUnits = false,
                LoadingPlaceholder = "image_loading.png",
                ErrorPlaceholder = "image_error.png"
            };
            IconImage.SetBinding(CachedImage.SourceProperty, "ImageUrl");

            Label NameLabel = new Label
            {
                Opacity = 0.5,
                HorizontalOptions = LayoutOptions.Fill,
                HorizontalTextAlignment = TextAlignment.Center,
                VerticalOptions = LayoutOptions.End,
            };
            NameLabel.SetBinding(Label.TextProperty, "FileName");
            Grid grd = new Grid
            {
                Padding = 3,
                ColumnDefinitions = {
                    new ColumnDefinition { Width = new GridLength (1, GridUnitType.Star) },
                },
                RowDefinitions = {
                    new RowDefinition { Height=GridLength.Star},
                },
            };
            grd.Children.Add(IconImage,0,0);
            grd.Children.Add(NameLabel, 0, 1);
        }
    }

我添加了FlowListView,FFImage等的所有依赖项,

以上代码只显示空白屏幕,不显示任何数据......

1 个答案:

答案 0 :(得分:0)

你的ListCell无需展示。

附加现有行以将内容设置为自定义ViewCell中的网格。

grd.Children.Add(IconImage,0,0);   
grd.Children.Add(NameLabel, 0, 1);
Content = grd;   <--- add this line

您是否按照作者的建议完成了以下操作?即除了PCL之外,您是否已将nuget库添加到平台,并为库添加了相应的初始化代码?

  

在使用FFImageLoading之前,必须将此行添加到特定于平台的项目(AppDelegate.cs,MainActivity.cs等):

CachedImageRenderer.Init();

这是我在IOS中得到的:

[Register("AppDelegate")]
public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
{
    public override bool FinishedLaunching(UIApplication app, NSDictionary options)
    {
        global::Xamarin.Forms.Forms.Init();

        FFImageLoading.Forms.Touch.CachedImageRenderer.Init();  <---- this line

        LoadApplication(new App());

        return base.FinishedLaunching(app, options);
    }
}