MVVMCross:ClearBindings()-如何在Touch中使用? [英] MVVMCross: ClearBindings() - How to use in Touch?

查看:52
本文介绍了MVVMCross:ClearBindings()-如何在Touch中使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ClearBindings()如何在MvvmCross中工作?

How does the ClearBindings() work in MvvmCross?

出于测试目的,我只是试图在ViewDidLoad()中为WeekSelectorView清除TableView的itemsSource.这是我尝试过的方法,但似乎无济于事.

As for testing purposes, I'm simply trying to clear my TableView's itemsSource in ViewDidLoad() for WeekSelectorView. Here's what I've tried, but nothing seems to work.

("this"是指我当前的WeekSelectorView实例)

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

当前,当我加载应用程序时,我的WeekSelectorView基于ViewModel的数据加载表.不过我想清除绑定,所以根本不应该有任何表.

Currently, when I load the app, my WeekSelectorView loads the table based off my ViewModel's data. I want to clear the binding though, so there shouldn't be any table at all.

this.ClearAllBindings();

上面的行有效,但是我不想清除所有绑定,我只想清除TableView的ItemsSource.

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

我目前有一个与.xib关联的WeekSelectorView. .xib中有一个TableView(以及其他用户控件).

I currently have a WeekSelectorView that has a .xib associated with it. In the .xib is a TableView (among other user controls).

我的WeekSelectorView将源设置为我自己的类"WeekSelectorTableSource".这个tablesource类根据ItemsSource绑定确定行数/节数.然后,它会在我的GetOrCreateCellsFor

My WeekSelectorView sets the source to my own class "WeekSelectorTableSource". This tablesource class determines the number of rows/sections based on the ItemsSource binding. It then creates some custom .xib cells and within 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;
    }

现在是为什么我要ClearBindings() ......

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

每次我重新加载表时,以前的绑定都会保留.因此,如果我绑定4个单元格,每个单元格具有3个绑定,则我的应用程序第一次将具有与该单元格关联的12个绑定.重新加载表格(仍然有4个单元格)后,将有24个绑定...然后是36、48等.

Every time I reload the table, my previous bindings persist. So, if I bind 4 cells with 3 bindings each, the first time my app will have 12 bindings associated with the cells. Once I reload the table (still with 4 cells) there will be 24 bindings....then 36, 48, etc.

这是诊断程序的一部分....

Here's part of the 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

重新加载表后,我只是收到了此消息,因此我想在每次TableView.ReloadData()之前清除绑定.

I'm just getting FLOODED with this message after I reload my table, so I wanted to clear my bindings each time before I TableView.ReloadData().

仔细考虑并与使用Windows本机绑定的同事交谈之后,我发现我做错了很多事情,长话短说,我不需要使用ClearBindings(view).

After thinking it over and talking with a coworker who works with Windows native bindings, I found out there was a LOT of things I was doing wrong, long story short, I don't need to use ClearBindings(view).

我让最外面的视图处理所有绑定,因此,当重新加载视图(表中的单元格)时,由于最外面的视图尚未被释放,因此绑定仍然存在.因此,我所有的子视图都从未处理过自己的绑定,这是一个重大错误.

I had my out-most view handling all the binding, so as a view was reloaded (aka cells in a table), the bindings still persisted because the out-most view hadn't been deallocated. So, all my subviews never handled their own bindings, which was a major mistake.

要解决此问题(以我想像的正确"方式进行更改),我将自定义单元格继承自MvxTableViewCell并添加了延迟绑定.

To fix change this (to the 'proper' way I'd imagine) I had my custom cell inherit from MvxTableViewCell and added a delay binding.

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

I tried this before but tried to create a set between <WeekSelectorCell, WeekSelectorViewModel> and tried to access (vm => vm.Options[ROW].Date), but this always failed. I finally learned I needed to create a set between <WeekSelectorCell, WeekViewModel> because Options[] is an ObservableCollection of WeekViewModel

就像我说的,长话短说,我不需要使用ClearBindings(view)

Like I said, long story short, I don't need to use ClearBindings(view)

推荐答案

ClearBindings()如何在MvvmCross中工作?

How does the ClearBindings() work in MvvmCross?

ClearBindings()ClearBindings(view)

每个MvxBindingContext维护3个单独的绑定"列表:

Each MvxBindingContext maintains 3 separate lists of 'bindings':

  1. 直接在上下文中创建的绑定列表
  2. 在子视图内创建的绑定的基于视图的查找表-当前,仅当在父绑定上下文中动态夸大子视图时,这些绑定才在Android绑定中使用.
  3. 正在等待首次调用DataContext=value
  4. 的动作列表(通常会继续创建绑定)
  1. a list of bindings created directly on the context
  2. a view-based lookup table of bindings created within child views - these are currently only used in Android bindings when child views are inflated dynamically within the parent binding context.
  3. a list of actions (which generally go on to create bindings) which are waiting for the first call to DataContext=value

其中第一个是当前在iOS中使用的主要版本-MvxBindingContext为该列表公开的唯一公共清除API是ClearAll.

The first of these is currently the main one used in iOS - and the only public clear API that MvxBindingContext exposes for that list is ClearAll.

第二个仅在Android中用于某些子视图Xml膨胀-而ClearBindings(view)允许将其用于该子视图.

The second one is used only in Android for some child view Xml inflations - and ClearBindings(view) allows these to be used for that.

所有这些背后的历史尤其与Android和iOS中的内存管理问题有关-尤其是试图确保我们跟踪并处置了所有绑定,包括在子视图,列表中创建的绑定等.

The history behind all of these is especially tied to lots of issues with memory management within both Android and iOS - especially with trying to make sure we tracked and disposed of all bindings, including those created within subviews, within lists, etc.

如果我们在这里可以找到一个很好的用例-从仅出于某些测试目的"的角度进行了一些扩展-那么ClearBindingsForObjectEnumerateBinding API可能会被认为是扩展的绑定上下文API-但肯定需要更严格的要求,以确保项目捕获到该API真正有用的功能.

If we can dig out a good use case here here - something a little expanded from "this is only for some testing purposes" - then a ClearBindingsForObject or EnumerateBinding API would be something that could be considered for an expanded binding context API - but it would definitely need a stronger requirement to make sure the project captured what the API is really useful for.

同时,我想如果愿意,您可以使用tableView作为查找来创建和注册绑定-例如像这样:

In the meantime, I guess you could create and register the binding using the tableView as a lookup if you wanted to - e.g. something like:

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

这将使您能够致电ClearBindings(tableView);

或者,如果您想停止单个绑定的工作,则可以提早Dispose进行操作-这将清除其源绑定-例如如果您这样做:

Alternatively, if you wanted to stop an individual binding from working then you could Dispose it early - that would clear it's source binding - e.g. if you did:

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

然后您可以稍后再执行以下操作:

then you could at some point later do something like:

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


或者-也许这就是我要遵循的方式(尽管这完全取决于您的用例)-然后将表放在自己的MvxView控件中可能会更容易-可以自己的BindingContext,您可以在其上调用ClearAllBindings().


Alternatively - and perhaps this is the way I'd go (although it depends on exactly what your use case is) - then it might be easier to just place your table in its own MvxView control - that can have it's own BindingContext which you can call ClearAllBindings() on.

有关MvxView的更多信息,请参见 http ://slodge.blogspot.co.uk/2013/06/n32-truth-about-viewmodels-starring.html

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

最后,我还可以考虑确定是否可以仅将绑定保留在适当的位置,而可以在ViewModel中清除ItemsSource.

Finally, I might also consider working out whether or not you could just keep the binding in place, but could instead clear the ItemsSource in your ViewModel.

这篇关于MVVMCross:ClearBindings()-如何在Touch中使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆