ClearBindings()
如何在MvvmCross中工作?MVVMCross:ClearBindings() - 如何使用聯繫人?
至於測試目的,我只是試圖清除我的TableView的的ItemsSource在ViewDidLoad()
爲WeekSelectorView。這是我嘗試過的,但似乎沒有任何工作。
(「本」指的是我目前WeekSelectorView的實例)
var source = new WeekSelectorTableSource(TableView, this);
TableView.Source = source;
var set = this.CreateBindingSet<WeekSelectorView, WeekSelectorViewModel>();
set.Bind(source).To(vm => vm.Options);
set.Apply();
//None of these work
this.ClearBindings (this);
this.ClearBindings (source);
this.ClearBindings (TableView.Source);
this.ClearBindings (source.ItemsSource);
this.ClearBindings ("ItemsSource");
this.ClearBindings ("source.ItemsSource");
this.ClearBindings ("TableView");
this.ClearBindings ("TableView.Source");
this.ClearBindings (TableView);
this.ClearBindings ("TableView.Source.ItemsSource");
this.ClearBindings (set);
this.ClearBindings ("set");
this.ClearBindings ("Options");
TableView.ReloadData();
目前,當我加載應用程序,我WeekSelectorView基於加載了我的視圖模型的數據表。我想清除綁定,所以根本不應該有任何表格。
this.ClearAllBindings();
上面的代碼行,但我不想清除所有的綁定,我只想清除我的TableView的ItemsSource。
編輯:
我現在有一個具有與之相關聯的一個的.xib WeekSelectorView。在.xib中是一個TableView(在其他用戶控件中)。
我WeekSelectorView設置源到我自己的類「WeekSelectorTableSource」。這個tableource類根據ItemsSource綁定來確定行/段的數量。然後,它創建了一些自定義的.xib細胞和我GetOrCreateCellsFor
protected override UITableViewCell GetOrCreateCellFor(UITableView tableView, NSIndexPath indexPath, object item)
{
var weekSelectorCell = WeekSelectorCell.Create();
var set = _WeekSelectorView.CreateBindingSet<WeekSelectorView, WeekSelectorViewModel>();
//Using string bindings since bindings with an index doesn't work
//ex: vm => vm.Options[indexPath.Row].Title
set.Bind(weekSelectorCell).For(wc => wc.Title).To(string.Format("{0}{1}{2}", Options, indexPath.Row, Title)).OneWay();
set.Bind(weekSelectorCell).For(wc => wc.Date).To(string.Format("{0}{1}{2}", Options, indexPath.Row, DateString)).OneWay();
set.Bind(weekSelectorCell).For(wc => wc.Hours).To(string.Format("{0}{1}{2}", Options, indexPath.Row, TotalHours)).WithConversion(new HoursDecimalToHoursMinutesConverter(), null).OneWay();
set.Apply();
return weekSelectorCell;
}
現在我爲什麼要ClearBindings()
的原因中......
我每次重裝該表,我以前的綁定依然存在。所以,如果我綁定4個單元格,每個單元格綁定3個,那麼我的應用程序第一次會有12個與單元格綁定的綁定。一旦我重新加載表(仍與4個細胞)將有24個綁定....然後36,48等
這裏的診斷的一部分....
2013-07-16 16:26:03.950 FCXiOSv2[569:21e03] MvxBind: Warning: 1259.41 Weak Target is null in MvxWithEventPropertyInfoTargetBinding - skipping set
2013-07-16 16:26:03.951 FCXiOSv2[569:21e03] MvxBind: Diagnostic: 1259.41 Receiving setValue to Week
2013-07-16 16:26:03.952 FCXiOSv2[569:21e03] MvxBind: Warning: 1259.42 Weak Target is null in MvxWithEventPropertyInfoTargetBinding - skipping set
2013-07-16 16:26:03.953 FCXiOSv2[569:21e03] MvxBind: Diagnostic: 1259.42 Receiving setValue to 7/8/13 - 7/14/13
2013-07-16 16:26:03.954 FCXiOSv2[569:21e03] MvxBind: Warning: 1259.42 Weak Target is null in MvxWithEventPropertyInfoTargetBinding - skipping set
我在我重新加載我的桌子後,剛剛得到了這條消息的泛濫,所以我想每次在我TableView.ReloadData()
之前清除綁定。
編輯:
思前想與誰用Windows自帶的綁定工作的同事交談後,我發現有東西很多,我做錯了,長話短說,我不t需要使用ClearBindings(view)
。
我有我最大的視圖處理所有的綁定,所以當一個視圖被重新加載(在表中的單元格),綁定仍然存在,因爲最外面的視圖還沒有被釋放。所以,我所有的子視圖都沒有處理他們自己的綁定,這是一個重大的錯誤。
要解決改變這種(對「正確」的方式我想像),我從我的MvxTableViewCell自定義單元格繼承,並增加了延遲綁定。
public WeekSelectorCell (IntPtr handle) : base (handle)
{
this.DelayBind (() =>
{
var set = this.CreateBindingSet<WeekSelectorCell, WeekViewModel>();
set.Bind(DateLabel).For(lbl => lbl.Text).To(vm => vm.DateString);
set.Bind(HoursLabel).For (lbl => lbl.Text).To(vm => vm.TotalHours).WithConversion(new HoursDecimalToHoursMinutesConverter(), null);
set.Bind(TitleLabel).For(lbl => lbl.Text).To(vm => vm.Title);
set.Apply();
});
}
我以前試過,但試圖創建<WeekSelectorCell, WeekSelectorViewModel>
之間的一組,並試圖存取(VM => vm.Options [ROW] .Date),但是這總是失敗。我終於學會我需要創建<WeekSelectorCell, WeekViewModel>
之間的一組,因爲Options[]
是WeekViewModel
的ObservableCollection
就像我說的,長話短說,我並不需要使用ClearBindings(view)
感謝您的長篇故事 - 非常有幫助...我仍然認爲可能有其他情況下刪除綁定可能是有用的(這是droid ClearBindings(視圖)方法的初衷 - 所以這可能會出現在iOS版本很快!謝謝你的幫助:) – Stuart