MVVMCross: ClearBindings () - How to use Touch?

How does ClearBindings() in MvvmCross?

As for testing, I'm just trying to clear the TableViewSource elements in ViewDidLoad() for WeekSelectorView. Here is what I tried, but nothing works.

("this" refers to my current instance of 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(); 

Currently, when I download the application, my WeekSelectorView loads a table based on my ViewModel data. I want to clear the binding, so there should be no table.

 this.ClearAllBindings(); 

The above line works, but I don't want to clear ALL the bindings, I just want to clear my TableView ItemsSource.


Edit:

I currently have a WeekSelectorView with its associated .xib. .Xib has a TableView (among other user controls).

My WeekSelectorView sets the source to my own WeekSelectorTableSource class. This tablesource class defines the number of rows / partitions based on the ItemsSource binding. Then it creates some custom .xib cells and inside my 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; } 

Now the reason I want ClearBindings() ......

Every time I reload the table, my previous bindings are saved. So, if I associate 4 cells with 3 bindings each, then for the first time my application will have 12 bindings associated with the cells. After reloading the table (still with 4 cells) there will be 24 bindings .... then 36, 48, etc.

Here is a part of Diagnostics ....

 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 

I just get a FLOODED message with this message after reloading the table, so I wanted to clear the bindings every time before I got TableView.ReloadData() .


Edit:

After thinking about it and talking to a colleague who works with native Windows connections, I found that there were a lot of things that I did wrong, a short history, I do not need to use ClearBindings(view) .

I had my external view processing the whole binding, since the view was reloaded (the so-called cells in the table), the bindings were still saved because the appearance was not released. So, all my subtitles never processed their own bindings, which was a serious mistake.

To fix this change (to β€œcorrect”, as I assume), I had my own inherited cell from MvxTableViewCell and a delay binding was added.

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

I tried this before, but tried to create a set between <WeekSelectorCell, WeekSelectorViewModel> and tried to access (vm => vm.Options [ROW] .Date), but it always worked. Finally, I found out that I need to create a set between <WeekSelectorCell, WeekViewModel> , because Options[] is an ObservableCollection of WeekViewModel

As I said, a long story, I do not need to use ClearBindings(view)

+4
source share
1 answer

How does ClearBindings () work in MvvmCross?

ClearBindings() ClearBindings(view)

Each MvxBindingContext supports 3 separate lists of "bindings":

  • list of bindings created directly in context
  • binding view-based lookup table created in child views - they are currently only used in Android bindings when child views are dynamically inflated in the context of parent binding.
  • list of actions (which usually go to create bindings) that wait for the first call to DataContext=value

The first one is currently the main one used in iOS, and the only open transparent API that MvxBindingContext provides for this list is ClearAll .

The second is used only in Android for some child views of Xml inflations - and ClearBindings(view) allows you to use them for this.

The story associated with this is especially related to many memory management problems in both Android and iOS - especially to make sure that we track and delete all bindings, including those created as part of subviews, in lists, etc. .d.


If we can find a good use case here β€” something has expanded a bit from β€œit's for some testing purposes only” —– then the ClearBindingsForObject or EnumerateBinding API would be something that could be considered for the extended context context API β€” but a stronger requirement will be required to ensure that the project captures what the API is really useful for.

In the meantime, I think you could create and register the binding using tableView as a search if you want - for example, something like:

 var bindings = MvxBindingSingletonCache.Instance.Binder.Bind(BindingContext.DataContext, tableView, "ItemsSource MySource"); this.RegisterBindingsFor(tableView, bindings); 

This will allow you to call ClearBindings(tableView);


Alternatively, if you want to stop an individual binding from working, you can Dispose it at an early stage - this will clear its binding to the source - for example. if you did this:

  _myDisposableBindings = MvxBindingSingletonCache.Instance.Binder.Bind(BindingContext.DataContext, tableView, "ItemsSource MySource"); this.AddBindings(_myDisposableBindings); 

then at some point you can do something like:

  foreach (var binding in _myDisposableBindings) { binding.Dispose(); } _myDisposableBindings = null; 

As an alternative - and perhaps this is how I go (although it depends on your use) - then it would be easier to just place the table in your own MvxView control, which can have this own BindingContext , which you can call ClearAllBindings() on.

For more information on MvxView see http://slodge.blogspot.co.uk/2013/06/n32-truth-about-viewmodels-starring.html


Finally, I could also think about whether you can just keep the binding in place, but instead can clear the ItemsSource in your ViewModel.

+3
source

Source: https://habr.com/ru/post/1491684/


All Articles