2

这是我的 ProductGroupTableViewcell 的代码,它必须从 NIB 文件加载:

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

    }

表视图源:

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

视图控制器;CrossCore 所以这里没有使用 UITableViewController:大多数导入这个区域#MVVM bindings的表包含我的 TableViewSource 和 ViewModel 的绑定。在HandleTouchUpInside事件中,我正在更改数据上下文。

首先,当视图加载时,我无法查看表数据。当我在里面做 touchup 时,我得到了正确的数据上下文。

我延迟了 Cell 的绑定,但是Set.Apply()应用程序崩溃并抛出 NullReference 异常:

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

    }
}

这里是 Nullreference 异常的堆栈跟踪:

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
4

0 回答 0