问题
Here the code for my ProductGroupTableViewcell, which must load from a NIB file:
//The cell must load from nib file
public partial class ProductGroupTableViewCell : MvxTableViewCell
{
public static readonly UINib Nib = UINib.FromName ("ProductGroupTableViewCell", NSBundle.MainBundle);
public static readonly NSString Key = new NSString ("ProductGroupTableViewCell");
public ProductGroupTableViewCell (IntPtr handle) : base (handle)
{
this.DelayBind(() => {
var set = this.CreateBindingSet<ProductGroupTableViewCell, ProductModel>();
set.Bind(imgProduct.Image).To(pm => pm.ProductImage1).WithConversion("UrltoImageConverter");
set.Bind(lblCurrencySymbol).To(pm => pm.CurrencySymbol);
set.Bind(lblProductCost).To(pm => pm.ProductExchangeCost).WithConversion("ExchangeCostToStringConverter");
set.Bind(lblProductName).To(pm => pm.ProductName);
set.Bind(lblProductQuantity).To(pm => pm.ProductQuantity);//.WithConversion("SpecialSymbolAtConverter");
set.Apply();
});
}
public static ProductGroupTableViewCell Create () //Don't know when is this called ??
{
return (ProductGroupTableViewCell)Nib.Instantiate (null, null) [0];
}
}
TableView source:
public class MvxProductTableViewSource: MvxTableViewSource
{
public MvxProductTableViewSource(UITableView tableView) : base(tableView)
{
//tableView.RegisterClassForCellReuse(typeof(ProductGroupTableViewCell), ProductGroupTableViewCell.Key);
tableView.RegisterNibForCellReuse (ProductGroupTableViewCell.Nib, ProductGroupTableViewCell.Key);
}
protected override UITableViewCell GetOrCreateCellFor(UITableView tableView, NSIndexPath indexPath, object item)
{
return tableView.DequeueReusableCell (ProductGroupTableViewCell.Key, indexPath);
}
}
View Controller; CrossCore so no usage of UITableViewController here: Most import this region #MVVM bindings
for table contains my binding of TableViewSource and ViewModel.
In HandleTouchUpInside
event, I am changing the data context.
First of all, I am not able to view the table data when the view loads. When I do touchup Inside I get the data context properly.
I delayed the binding of Cell but with Set.Apply()
, the application crashes throwing a NullReference exception:
public partial class ProductGroupDetailController : UIViewController, IMvxBindable
{
private ProductGroupDetailViewModel2 productGrpViewModel = new ProductGroupDetailViewModel2();
UIScrollView scrollView;
List<UIButton> buttonlist;
private String[] categorylist ;
public ProductGroupDetailController () : base ("ProductGroupDetailController", null)
{
buttonlist=new List<UIButton>();
this.CreateBindingContext();
DataContext = productGrpViewModel.ProductGroup;
}
#region forBinding
public IMvxBindingContext BindingContext { get; set; }
protected override void Dispose(bool disposing)
{
if (disposing)
{
BindingContext.ClearAllBindings();
}
base.Dispose(disposing);
}
public object DataContext
{
get { return BindingContext.DataContext; }
set { BindingContext.DataContext = value; }
}
#endregion
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
#region code for horizontal scroll list
if (productGrpViewModel != null && productGrpViewModel.ProductGroup.Count > 0)
{
categorylist = productGrpViewModel.ProductGroup.Select (p => p.CategoryName).ToArray();
// Perform any additional setup after loading the view, typically from a nib.
float h = 30.0f;
float w = 100.0f;
float padding = 10.0f;
int n = categorylist.Length;
View.BackgroundColor=UIColor.FromPatternImage(new UIImage("background.png"));
scrollView=new UIScrollView {
Frame = new RectangleF (0, 126, View.Frame.Width,h + 2 * padding),
ContentSize = new SizeF ((w + padding) * n, h),
AutoresizingMask = UIViewAutoresizing.FlexibleWidth,
ShowsHorizontalScrollIndicator = false
};
for (int i=0; i<n; i++)
{
var button = UIButton.FromType (UIButtonType.RoundedRect);
button.SetTitle (categorylist[i].ToString (), UIControlState.Normal);
button.SetTitleColor (UIColor.White, UIControlState.Normal);
button.Font = UIFont.FromName ("Helvetica", 18f);
button.Frame = new RectangleF (padding * (i + 1) + (i * w),padding, w, h);
scrollView.AddSubview (button);
buttonlist.Add (button);
buttonlist [i].TouchUpInside += HandleTouchUpInside;
}
buttonlist [0].BackgroundColor = UIColor.LightGray;
View.AddSubview (scrollView);
#endregion horizontal scroollist
#MVVM bindings for table
var source = new MvxProductTableViewSource (ProductTable);
ProductTable.Source = source;
var set = this.CreateBindingSet<ProductGroupDetailController, ProductCategoryModel2>();
set.Bind(source).For(s => s.ItemsSource).To(vm => vm.Products);
set.Apply();
//source.ReloadTableData ();
}
}
void HandleTouchUpInside(object sender,EventArgs e)
{
//based on category title update the UITableView with new products
List<ProductCategoryModel2> t = productGrpViewModel.ProductGroup.ToList ();
//set DataContext to new product group
DataContext = t.FirstOrDefault(catname => catname.CategoryName == title);
ProductTable.PagingEnabled=true;
ProductTable.Bounces=true;
ProductTable.PagingEnabled=true;
ProductTable.ReloadData ();
}
}
}
Here the stack trace of the Nullreference exception:
System.NullReferenceException: Object reference not set to an instance of an object
at Cirrious.MvvmCross.Binding.Bindings.Target.Construction.MvxTargetBindingFactoryRegistry.TryCreateSpecificFactoryBinding (System.Object target, System.String targetName, IMvxTargetBinding& binding) [0x00000] in <filename unknown>:0
at Cirrious.MvvmCross.Binding.Bindings.Target.Construction.MvxTargetBindingFactoryRegistry.CreateBinding (System.Object target, System.String targetName) [0x00000] in <filename unknown>:0
at Cirrious.MvvmCross.Binding.Bindings.MvxFullBinding.CreateTargetBinding (System.Object target) [0x00000] in <filename unknown>:0
at Cirrious.MvvmCross.Binding.Bindings.MvxFullBinding..ctor (Cirrious.MvvmCross.Binding.MvxBindingRequest bindingRequest) [0x00000] in <filename unknown>:0
at Cirrious.MvvmCross.Binding.Binders.MvxFromTextBinder.BindSingle (Cirrious.MvvmCross.Binding.MvxBindingRequest bindingRequest) [0x00000] in <filename unknown>:0
at Cirrious.MvvmCross.Binding.Binders.MvxFromTextBinder+<>c__DisplayClass1.<Bind>b__0 (Cirrious.MvvmCross.Binding.Bindings.MvxBindingDescription description) [0x00000] in <filename unknown>:0
at System.Linq.Enumerable+<CreateSelectIterator>c__Iterator10`2[Cirrious.MvvmCross.Binding.Bindings.MvxBindingDescription,Cirrious.MvvmCross.Binding.Bindings.IMvxUpdateableBinding].MoveNext () [0x00059] in /Developer/MonoTouch/Source/mono/mcs/class/System.Core/System.Linq/Enumerable.cs:2269
at Cirrious.MvvmCross.Binding.BindingContext.MvxBindingContextOwnerExtensions.AddBindings (IMvxBindingContextOwner view, IEnumerable`1 bindings, System.Object clearKey) [0x00000] in <filename unknown>:0
at Cirrious.MvvmCross.Binding.BindingContext.MvxBindingContextOwnerExtensions.AddBindings (IMvxBindingContextOwner view, System.Object target, IEnumerable`1 bindingDescriptions, System.Object clearKey) [0x00000] in <filename unknown>:0
at Cirrious.MvvmCross.Binding.BindingContext.MvxBindingContextOwnerExtensions.AddBinding (IMvxBindingContextOwner view, System.Object target, Cirrious.MvvmCross.Binding.Bindings.MvxBindingDescription bindingDescription, System.Object clearKey) [0x00000] in <filename unknown>:0
at Cirrious.MvvmCross.Binding.BindingContext.MvxBaseFluentBindingDescription`1[MonoTouch.UIKit.UIImage].Apply () [0x00000] in <filename unknown>:0
at Cirrious.MvvmCross.Binding.BindingContext.MvxFluentBindingDescriptionSet`2[OnBoardPOS.IOS.ProductGroupTableViewCell,OnBoardPOS.WP8.Framework.Model.ProductModel].Apply () [0x00000] in <filename unknown>:0
at OnBoardPOS.IOS.ProductGroupTableViewCell.<ProductGroupTableViewCell>m__1D () [0x001c3] in /Users/admin/Desktop/leelaswork/withouttabs/OnBoardPOS.IOS/views/ProductGroupTableViewCell.cs:28
at Cirrious.MvvmCross.Binding.BindingContext.MvxBindingContext.OnDataContextChange () [0x00000] in <filename unknown>:0
at Cirrious.MvvmCross.Binding.BindingContext.MvxBindingContext.set_DataContext (System.Object value) [0x00000] in <filename unknown>:0
at Cirrious.MvvmCross.Binding.Touch.Views.MvxTableViewCell.set_DataContext (System.Object value) [0x00000] in <filename unknown>:0
at Cirrious.MvvmCross.Binding.Touch.Views.MvxBaseTableViewSource.GetCell (MonoTouch.UIKit.UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath) [0x00000] in <filename unknown>:0
来源:https://stackoverflow.com/questions/19813685/binding-mvvmcrosscore-tableview-nullreference-exception