2016-06-22 47 views
0

我有一个Xamarin表单列表视图的应用程序,该列表由ObservableCollection提供,用于列出从我的服务器获取的用户游戏。我第一次加载它就能很好地工作。如果我以任何方式修改现有的ObservableCollection,我的应用程序会崩溃并出现NullException。我已经缩小到我的自定义ViewCell(GameCell),我已经在下面发布了。如果我评论了一条线向底部:ListView在使用我的自定义单元格重新加载时崩溃

this.SetBinding(IsEnabledProperty, "Enabled"); 

然后,它不再崩溃更新的ObservableCollection,但后来我不再获得的功能,以启用一些细胞和禁用一些细胞。我猜测它试图将isEnabled属性设置为不再存在的单元格,因为它在重新加载时已被删除。任何想法如何做到这一点与崩溃?顺便说一句,这工作正常在Android上,只是没有在iOS

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; } 

    } 
} 
+0

原来更新到最新的软件包修复了崩溃! – Daniel

回答

0

原来更新到最新的软件包固定的崩溃。

相关问题