ListView与我的自定义单元格重新加载时崩溃

时间:2016-06-22 04:14:33

标签: xamarin xamarin.forms

我有一个带有Xamarin表单列表视图的应用程序,它由ObservableCollection提供,用于列出我从服务器获取的用户游戏。我第一次加载它就可以了。如果我以任何方式修改现有的ObservableCollection我的应用程序崩溃与NullException。我把它缩小到我的自定义ViewCell(GameCell),我在下面发布了。如果我在底部评论一行:

using System;
using Xamarin.Forms;
using FFImageLoading.Forms;

namespace Upwords
{
    public class GameCell : ViewCell
    {
        public GameCell ()
        {

            var icon = new CachedImage () {
                Aspect = Aspect.AspectFit,
                HorizontalOptions = LayoutOptions.Fill,
                VerticalOptions = LayoutOptions.Fill,
                BackgroundColor = AppStyle.IconColor,
                IsVisible = false,
            };
            icon.SetBinding (CachedImage.SourceProperty, "Icon");
            icon.SetBinding (CachedImage.LoadingPlaceholderProperty, "IconPlaceholder");
            icon.SetBinding (CachedImage.ErrorPlaceholderProperty, "IconPlaceholder");

            icon.SetBinding (CachedImage.WidthRequestProperty, "Height");
            icon.SetBinding (CachedImage.HeightRequestProperty, "Height");
            icon.Success += (object sender, CachedImageEvents.SuccessEventArgs e) => {
                icon.FadeAnimationEnabled = false;
                if(icon.Source.GetType() == typeof(Xamarin.Forms.UriImageSource)) {
                    icon.BackgroundColor = Color.Transparent;
                }
            };

            icon.PropertyChanged += (object sender, System.ComponentModel.PropertyChangedEventArgs e) => {

                if(icon.Source != null)
                    icon.IsVisible = true;
                else
                    icon.IsVisible = false;

            };

            var nameLabel = new Label () {
                FontFamily = "Helvetica",
                FontAttributes = FontAttributes.Bold,
                FontSize = Device.GetNamedSize (NamedSize.Medium, typeof(Label)),
                TextColor = Color.Black,
            };
            nameLabel.SetBinding (Label.TextProperty, "Name");

            //Hide label if it's blank
            nameLabel.PropertyChanged += (object sender, System.ComponentModel.PropertyChangedEventArgs e) => {
                if(nameLabel.Text == "")
                    nameLabel.IsVisible = false;
                else
                    nameLabel.IsVisible = true;
            };

            var detailsLabel = new Label () {
                FontFamily = "Helvetica",
                FontSize = Device.GetNamedSize (NamedSize.Small, typeof(Label)),
                FontAttributes = FontAttributes.Bold,
                TextColor = Color.FromHex ("#666"),
                IsVisible = false,

            };
            detailsLabel.SetBinding (Label.TextProperty, "Details");

            //Hide label if it's blank
            detailsLabel.PropertyChanged += (object sender, System.ComponentModel.PropertyChangedEventArgs e) => {
                if(string.IsNullOrEmpty( detailsLabel.Text ) )
                    detailsLabel.IsVisible = false;
                else
                    detailsLabel.IsVisible = true;
            };

            var textLayout = new StackLayout {
                Padding = new Thickness (5, 0, 0, 0),
                Spacing = 0,
                HorizontalOptions = LayoutOptions.FillAndExpand,
                VerticalOptions = LayoutOptions.CenterAndExpand,
                Children = { nameLabel, detailsLabel}
            };

            var optionSwitch = new Switch() {
                HorizontalOptions = LayoutOptions.End,
                VerticalOptions = LayoutOptions.Center,
                IsVisible = false,
            };
            optionSwitch.SetBinding (Switch.IsVisibleProperty, "IsSwitch");

            var statusLayout = new StackLayout {
                Padding = new Thickness (10, 10, 10, 10),
                Orientation = StackOrientation.Horizontal,
                HorizontalOptions = LayoutOptions.FillAndExpand,
                Children = { icon, textLayout, optionSwitch}
            };
            statusLayout.SetBinding (StackLayout.HeightRequestProperty, "Height");

            var separatorBox = new BoxView {
                HeightRequest = 1,
                HorizontalOptions = LayoutOptions.FillAndExpand,
                BackgroundColor = Color.FromRgb(.75,.75,.75),
            };
            separatorBox.SetBinding (BoxView.HeightRequestProperty, "SeparatorHeight");

            var separatorBoxLayout = new StackLayout {
                Padding = new Thickness (50, 0, 0, 0),
                HorizontalOptions = LayoutOptions.FillAndExpand,
                Children = { separatorBox }
            };

            var cellLayout = new StackLayout {
                Spacing = 0,
                Padding = new Thickness (0, 0, 0, 0),
                Orientation = StackOrientation.Vertical,
                HorizontalOptions = LayoutOptions.FillAndExpand,
                BackgroundColor = Color.White,
                Opacity = 1.0,
                Children = { statusLayout, separatorBoxLayout }
            };
            cellLayout.SetBinding (StackLayout.OpacityProperty, "Opacity");

            var headerFrame = new ListViewLabel () {

            };
            headerFrame.SetBinding (ListViewLabel.IsVisibleProperty, "IsLabel");
            headerFrame.SetBinding (ListViewLabel.TextProperty, "Name");
            headerFrame.SetBinding (ListViewLabel.HeightRequestProperty, "Height");

            headerFrame.PropertyChanged += (object sender, System.ComponentModel.PropertyChangedEventArgs e) => {
                if(headerFrame.IsVisible)
                    cellLayout.IsVisible = false;
                else
                    cellLayout.IsVisible = true;
            };

            var paddedLayout = new StackLayout {
                Spacing = 0,
                Orientation = StackOrientation.Horizontal,
                HorizontalOptions = LayoutOptions.FillAndExpand,
                VerticalOptions = LayoutOptions.FillAndExpand,
                Children = { headerFrame, cellLayout  }
            };

            this.SetBinding(IsEnabledProperty, "Enabled");
            this.View = paddedLayout;

        }

    }

    public class GameCellData
    {

        public string Icon { get; set; }

        public string IconPlaceholder { get; set; }

        public string Name { get; set; }

        public string Details { get; set; }

        public int Height { get; set; }

        public int SeparatorHeight { get; set; }

        public bool Enabled { get; set; }

        public double Opacity { get; set; }

        public bool IsLabel { get; set; }

        public bool IsSwitch { get; set; }

    }
}

然后它不再崩溃更新ObservableCollection,但后来我不再获得启用某些单元格并禁用某些单元格的功能。我猜它正在尝试将isEnabled属性设置为不再存在的单元格,因为它在我的重新加载时已被删除。任何想法如何与崩溃?顺便说一句,这在Android上运行良好,而不是在iOS上运行

watchKit

1 个答案:

答案 0 :(得分:0)

更新到修复崩溃的最新软件包。