2013-10-10 141 views
2

我試圖爲我的MvxTableViewController創建自定義單元格,如Stuart Lodge視頻中所示N = 3 & N = 6.5但它無法綁定數據。Mvvmcross MvxSimpleTableViewSource綁定

這裏的工作版本(帶有基本單元)

using Cirrious.MvvmCross.Binding.BindingContext; 
using Cirrious.MvvmCross.Binding.Touch.Views; 
using Cirrious.MvvmCross.Touch.Views; 
using Cirrious.MvvmCross.ViewModels; 
using MonoTouch.Foundation; 
using MonoTouch.UIKit; 
using Next.Client.Application.Core.Entities.Observations; 
using Next.Client.Application.Core.ViewModels; 
using System; 
using System.Collections.Generic; 
using System.Drawing; 
using System.Linq; 
using System.Text; 

namespace Next.Client.Application.iOS.Views 
{ 
    [Register("ObsSearchView")] 
    public partial class ObsSearchView : MvxTableViewController 
    { 
     public static readonly NSString CellIdentifier = new NSString("ObservationCell"); 

     public ObsSearchView() 
     { 
     } 

     public ObsSearchView(IntPtr handle) 
      : base(handle) 
     { 
     } 

     public override void ViewDidLoad() 
     { 
      base.ViewDidLoad(); 
      Request = new MvxViewModelRequest<ObsSearchViewModel>(null, null, new MvxRequestedBy()); 
      LoadData(); 
     } 

     public override void ViewWillAppear(bool animated) 
     { 
      base.ViewWillAppear(animated); 
      Title = NSBundle.MainBundle.LocalizedString("Liste Observations", "Liste Observations"); 
     } 

     public override void ViewWillDisappear(bool animated) 
     { 
      Title = NSBundle.MainBundle.LocalizedString("Back", "Liste Observations"); 
      base.ViewDidDisappear(animated); 
     } 

     public void LoadData() 
     {     
      var source = new MvxStandardTableViewSource(
       TableView, 
       UITableViewCellStyle.Subtitle, 
       CellIdentifier, 
       "TitleText BrutText; DetailText DateTimeHuman", 
       UITableViewCellAccessory.DisclosureIndicator 
      ); 

      TableView.Source = source; 

      var set = this.CreateBindingSet<ObsSearchView, Core.ViewModels.ObsSearchViewModel>(); 

      set.Bind(source).To(vm => vm.Observations); 
      set.Bind(source).For(s => s.SelectionChangedCommand).To(vm => vm.SelectedObsCommand); 
      set.Apply(); 

      TableView.ReloadData(); 
     } 
    } 
} 

,這裏的修改版本添加自定義單元格:

using Cirrious.MvvmCross.Binding.BindingContext; 
using Cirrious.MvvmCross.Binding.Touch.Views; 
using Cirrious.MvvmCross.Touch.Views; 
using Cirrious.MvvmCross.ViewModels; 
using MonoTouch.Foundation; 
using MonoTouch.UIKit; 
using Next.Client.Application.Core.Entities.Observations; 
using Next.Client.Application.Core.ViewModels; 
using System; 
using System.Collections.Generic; 
using System.Drawing; 
using System.Linq; 
using System.Text; 

namespace Next.Client.Application.iOS.Views 
{ 
    [Register("ObsSearchView")] 
    public partial class ObsSearchView : MvxTableViewController 
    { 
     public static readonly NSString CellIdentifier = new NSString("ObservationCell"); 

     public ObsSearchView() 
     { 
     } 

     public ObsSearchView(IntPtr handle) 
      : base(handle) 
     { 
     } 

     public override void ViewDidLoad() 
     { 
      base.ViewDidLoad(); 
      Request = new MvxViewModelRequest<ObsSearchViewModel>(null, null, new MvxRequestedBy()); 
      LoadData(); 
     } 

     public override void ViewWillAppear(bool animated) 
     { 
      base.ViewWillAppear(animated); 
      Title = NSBundle.MainBundle.LocalizedString("Liste Observations", "Liste Observations"); 
     } 

     public override void ViewWillDisappear(bool animated) 
     { 
      Title = NSBundle.MainBundle.LocalizedString("Back", "Liste Observations"); 
      base.ViewDidDisappear(animated); 
     } 

     public void LoadData() 
     { 
      var source = new MvxSimpleTableViewSource(
       TableView, 
       ObservationCell.Key, 
       ObservationCell.Key 
      ); 
      TableView.Source = source; 

      TableView.ReloadData(); 
     } 
    } 
} 

與自定義單元格類

using System; 
using System.Drawing; 
using MonoTouch.Foundation; 
using MonoTouch.UIKit; 
using Cirrious.MvvmCross.Binding.Touch.Views; 
using Cirrious.MvvmCross.Binding.BindingContext; 
using Next.Client.Application.Core.ViewModels; 
using Next.Client.Application.Core.Entities.Observations; 

namespace Next.Client.Application.iOS 
{ 
    public partial class ObservationCell : MvxTableViewCell 
    { 
     public static readonly UINib Nib = UINib.FromName ("ObservationCell", NSBundle.MainBundle); 
     public static readonly NSString Key = new NSString ("ObservationCell"); 

     public ObservationCell (IntPtr handle) : base (handle) 
     { 
      this.DelayBind(() => { 
       var set = this.CreateBindingSet<ObservationCell, Observation>(); 
       set.Bind(MainLbl).To(observation => observation.BrutText); 
       set.Bind(SubLeftLbl).To(observation => observation.DateTimeHuman); 
       set.Bind(SubRightLbl).To(observation => observation.Praticien.Personne.DisplayFullName); 
       set.Apply(); 
      }); 
     } 

     public static ObservationCell Create() 
     { 
      return (ObservationCell)Nib.Instantiate (null, null) [0]; 
     } 
    } 
} 

,最後是觀察實體

using Next.Client.Application.Core.Helpers; 
using System; 
using System.Collections.Generic; 
using System.Collections.ObjectModel; 
using System.Linq; 
using System.Text; 
using Next.Client.Application.Core.Entities; 
using System.Globalization; 

namespace Next.Client.Application.Core.Entities.Observations 
{ 
    public class Observation : Entity 
    { 
     #region Constructors 
     #endregion 

     #region Constantes 
     private readonly List<String> _month = new List<String>{ 
       "", 
       "jan.", 
       "fev.", 
       "mars", 
       "avr.", 
       "mai", 
       "juin", 
       "jui.", 
       "aout", 
       "sep.", 
       "oct.", 
       "nov.", 
       "dec." 
      }; 
     #endregion 


     #region Simple Properties 

     #region Foreign keys 

     private int _praticienId; 
     public int PraticienId 
     { 
      get { return _praticienId; } 
      set { _praticienId = value; OnPropertyChange(() => PraticienId); } 
     } 

     private int _sejourId; 
     public int SejourId 
     { 
      get { return _sejourId; } 
      set { _sejourId = value; OnPropertyChange(() => SejourId); } 
     } 

     private int _categoryId; 
     public int CategoryId 
     { 
      get { return _categoryId; } 
      set { _categoryId = value; OnPropertyChange(() => CategoryId); } 
     } 
     #endregion 

     private DateTime _dateTime; 
     public DateTime DateTime 
     { 
      get { return _dateTime; } 
      set { _dateTime = value; OnPropertyChange(() => DateTime); OnPropertyChange(() => DateTimeHuman); } 
     } 

     public String DateTimeHuman 
     { 
      get 
      { 
       CultureInfo culture = new CultureInfo("fr-FR"); 
       return DateTime.ToString("f", culture); 
      } 
     } 

     public string DisplayDateTime 
     { 
      get { return string.Format("{0} {1} {2} {3}h{4:00}", _dateTime.Day, _month[_dateTime.Month], _dateTime.Year, _dateTime.Hour, _dateTime.Minute); } 
     } 
     private int _status; 
     public int Status 
     { 
      get { return _status; } 
      set { _status = value; OnPropertyChange(() => Status); } 
     } 

     private int _type; 
     public int Type 
     { 
      get { return _type; } 
      set { _type = value; OnPropertyChange(() => Type); } 
     } 

     private bool _private; 
     public bool Private 
     { 
      get { return _private; } 
      set { _private = value; OnPropertyChange(() => Private); OnPropertyChange(() => DisplayPrivacy); } 
     } 

     public string DisplayPrivacy 
     { 
      get { if (_private) return "OUI"; else return "NON"; } 
     } 

     private string _brutText; 
     public string BrutText 
     { 
      get { return _brutText; } 
      set { _brutText = value; OnPropertyChange(() => BrutText); } 
     } 

     #endregion 

     #region Navigation Properties 

     private Praticien _praticien; 
     public Praticien Praticien 
     { 
      get { return _praticien; } 
      set 
      { 
       Praticien old = _praticien; 
       _praticien = value; 
       CollectionHelper.ManyToOne(this, old, value, _GetManyToOneCollection); 
       OnPropertyChange(() => Praticien); 
      } 
     } 

     private Sejour _sejour; 
     public Sejour Sejour 
     { 
      get { return _sejour; } 
      set 
      { 
       Sejour old = _sejour; 
       _sejour = value; 
       CollectionHelper.ManyToOne(this, old, value, _GetManyToOneCollection); 
       OnPropertyChange(() => Sejour); 
      } 
     } 

     private Category _category; 
     public Category Category 
     { 
      get { return _category; } 
      set 
      { 
       Category old = _category; 
       _category = value; 
       CollectionHelper.ManyToOne(this, old, value, _GetManyToOneCollection); 
       OnPropertyChange(() => Category); 
      } 
     } 

     private ObservableCollection<BinaryObservation> _datas; 
     public ObservableCollection<BinaryObservation> Datas 
     { 
      get 
      { 
       if (_datas == null) 
       { 
        _datas = new ObservableCollection<BinaryObservation>(); 
        CollectionHelper.OneToMany(this, _GetOneToMany, _SetOneToMany, _datas); 

       } 
       return _datas; 
      } 
      set { _datas = value; OnPropertyChange(() => Datas); } 
     } 

     #endregion 

     #region Association Fixup 

     private static ObservableCollection<Observation> _GetManyToOneCollection(Sejour sejour) 
     { 
      return sejour.Observations; 
     } 

     private static ObservableCollection<Observation> _GetManyToOneCollection(Category category) 
     { 
      return category.Observations; 
     } 

     private static ObservableCollection<Observation> _GetManyToOneCollection(Praticien praticien) 
     { 
      return praticien.Observations; 
     } 

     #region binary observation (datas) 

     private static Observation _GetOneToMany(BinaryObservation binaryObservation) 
     { 
      return binaryObservation.Observation; 
     } 

     private static void _SetOneToMany(BinaryObservation binaryObservation, Observation observation) 
     { 
      binaryObservation.Observation = observation; 
     } 

     #endregion 
     #endregion 

    } 
} 

當我建立我得到沒有錯誤,我也可以運行調試沒有任何錯誤,但是當使用自定義單元沒有任何顯示,所以它似乎綁定沒有以正確的方式完成。

感謝您的時間

+0

我需要從列表中選擇項目,所以我綁定的源SelectedItem屬性,但它給空每次。我這樣寫:set.Bind(source).For(P => P.SelectedItem).To(vm => vm.SelectedOrganization);如果它是錯誤的,那麼請告訴我如何獲得選定的項目? –

回答

1

在你修改ObsView你從來沒有真正結合ItemsSource的TableSource - 這樣的表源不知道什麼收藏展示

在N + 1只3小貓教程這是通過使用完成的:

 var set = this.CreateBindingSet<FirstView, FirstViewModel>(); 
     set.Bind(source).To(vm => vm.Kittens); 
     set.Apply(); 

參見https://github.com/MvvmCross/NPlus1DaysOfMvvmCross/blob/master/N-03-KittenView_Mac/KittenView.Touch/Views/FirstView.cs#L24

類似地,在N = 6:

 var set = this.CreateBindingSet<FirstView, Core.ViewModels.FirstViewModel>(); 
     // ... 
     set.Bind(source).To(vm => vm.Results); 
     // ... 
     set.Apply(); 

https://github.com/MvvmCross/NPlus1DaysOfMvvmCross/blob/master/N-07-BooksPlus/Books.Touch/Views/FirstView.cs#L45

+0

太棒了!我錯過了基本的綁定:)這真的是一個初學者的錯誤。非常感謝您的回答(順便說一下,您的視頻對mvvmcross啓動器有很大的幫助;)) – kitensei

+0

我需要從列表中選擇項目,因此我綁定了源SelectedItem屬性,但它每次都會給出空值。我這樣寫: set.Bind(source).For(P => P.SelectedItem).To(vm => vm.SelectedOrganization); 如果它是錯誤的,那麼請告訴我如何獲得選定的項目? –