Xamarin.Forms使用Android加载大型数据集

时间:2018-06-26 11:22:30

标签: c# android xamarin.forms

我一直在研究Xamarin Forms项目,但无法将数据加载到Android应用程序中。它适用于iOS和UWP,但在Android上却需要永远。这是在两个不同的屏幕上发生的,但我假设问题是相同的。我敢肯定有比我正在使用的更好的数据加载方法。

这是我在Android上遇到的第一个屏幕(UWP的屏幕截图): Merchandise Listing

它将每个类别的15种产品加载到水平滚动器中。 “查看全部”链接将该类别中的所有产品加载到垂直滚动网格中。那是我遇到的第二个屏幕。

加载此视图的代码为:

     var scroller = new TLScrollView
     {
          Orientation = ScrollOrientation.Horizontal,
          ItemTemplate = new DataTemplate(() => ViewFactory.Merchandise.Invoke()),
          HeightRequest = 320,
          Padding = new Thickness(0, 0, 0, 10)
     };

     scroller.SetBinding(TLScrollView.ItemsSourceProperty, nameof(items));
     scroller.ItemsSource = items.OrderByDescending(d => d.Cost).Take(15);

ViewFactory代码为:

public static Func<View> Merchandise { get; } = () =>
{
    var mainGrid = new Grid() { WidthRequest = 250, Margin = 5 };

    mainGrid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) });
    mainGrid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(25, GridUnitType.Absolute) });
    mainGrid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(25, GridUnitType.Absolute) });
    mainGrid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(25, GridUnitType.Absolute) });
    mainGrid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(35, GridUnitType.Absolute) });

    var nameLabel = new Label() { Text = "Casket Name", LineBreakMode = LineBreakMode.TailTruncation, HorizontalTextAlignment = TextAlignment.Center };
    var materialLabel = new Label() { Text = "MaterialName", HorizontalTextAlignment = TextAlignment.Center };
    var costLabel = new Label() { Text = "Cost", HorizontalTextAlignment = TextAlignment.Center };

    nameLabel.SetBinding(Label.TextProperty, "ItemName");
    materialLabel.SetBinding(Label.TextProperty, "Material");
    costLabel.SetBinding(Label.TextProperty, "CostFormatted");

    GlobalModel globalModel = App.Current.MainPage.BindingContext as GlobalModel;

    var catalogImage = new CachedImage
    {
        HorizontalOptions = LayoutOptions.FillAndExpand,
        VerticalOptions = LayoutOptions.FillAndExpand,
        Aspect = Aspect.AspectFit,
        LoadingPlaceholder = "MAFSLoader.gif",
        CacheDuration = TimeSpan.FromDays(10),
        RetryCount = 1,
        RetryDelay = 100
    };

    catalogImage.BindingContextChanged += (s, o) => {
        catalogImage.Source = null;
        var item = catalogImage.BindingContext as CatalogItem;

        if (item == null) return;

        catalogImage.Source = item.SmallURL;
    };

    var buttonStyle = (Style)Application.Current.Resources["ButtonStyle"];
    var addToCartButton = new Button { Text = "Add to Cart", BackgroundColor = Color.FromHex("#486E8E"), WidthRequest = 250, Style = buttonStyle };
    addToCartButton.SetBinding(Button.CommandParameterProperty, "ItemID");

    var imgTapGesture = new TapGestureRecognizer()
    {
        Command = new Command(() =>
        {
            if (addToCartButton.CommandParameter == null)
            {
                addToCartButton.SetBinding(Button.CommandParameterProperty, "ItemId");
            }

            var dictionary = new Dictionary<string, string>();
            dictionary.Add(globalModel.CurrentFuneralHomeKey, addToCartButton.CommandParameter.ToString());
            globalModel.ProductDetail.Execute(dictionary);
        })
    };
    catalogImage.GestureRecognizers.Add(imgTapGesture);

    addToCartButton.Clicked += new EventHandler(async delegate (Object o, EventArgs a)
    {
        var button = ((Button)o);

        globalModel.AddToCart.Execute(button.BindingContext);

        if (button.BindingContext.GetType() == typeof(Data.Corner))
        {
            await App.Current.MainPage.DisplayAlert("Shopping Cart", "You've added " + (button.BindingContext as Data.Corner).ItemName + " to your cart.", null, "OK");
        }
    });

    mainGrid.Children.Add(catalogImage, 0, 0);
    mainGrid.Children.Add(nameLabel, 0, 1);
    mainGrid.Children.Add(materialLabel, 0, 2);
    mainGrid.Children.Add(costLabel, 0, 3);
    mainGrid.Children.Add(addToCartButton, 0, 4);


    return mainGrid;
};

如果有人能指出我正确的方向,我将不胜感激!

1 个答案:

答案 0 :(得分:0)

有趣。我刚刚完成了TLSrollview的实现。似乎没有元素的回收,并且可能缺乏一些优化(只有在调试时才能看到)。我看到两个选择:

  • 选项1:创建一个启用了回收策略的列表视图。为了获得更好的性能,请同时设置行高。列表视图中的一项将是一行。列表视图的优点是回收策略(尤其是在Android上)的性能提高。请注意,在iOS和Android上,最好将其设置为开启或关闭。
  • 选项2:创建自定义布局。可能有点矫kill过正。