xamarin.ios - MVVMCross:ClearBindings() - 如何在 Touch 中使用?

标签 xamarin.ios mvvmcross

ClearBindings() 如何在 MvvmCross 工作?

至于测试目的,我只是想清除 ViewDidLoad() 中的 TableView 的 itemsSource对于 WeekSelectorView。这是我尝试过的,但似乎没有任何效果。

(“this”指的是我当前的 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 的数据加载表。我想清除绑定(bind),所以根本不应该有任何 table 。
this.ClearAllBindings();

上面的行有效,但我不想清除所有绑定(bind),我只想清除我的 TableView 的 ItemsSource。

编辑:

我目前有一个 WeekSelectorView,它有一个 .xib 与之关联。在 .xib 中是一个 TableView(在其他用户控件中)。

我的 WeekSelectorView 将源设置为我自己的类“WeekSelectorTableSource”。此 tablesource 类根据 ItemsSource 绑定(bind)确定行/节的数量。然后它会在我的 GetOrCreateCellsFor 中创建一些自定义 .xib 单元格
    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()的原因……

每次我重新加载表时,我以前的绑定(bind)都会持续存在。因此,如果我绑定(bind) 4 个单元格,每个单元格有 3 个绑定(bind),那么我的应用程序第一次将有 12 个与单元格关联的绑定(bind)。一旦我重新加载表格(仍然有 4 个单元格),就会有 24 个绑定(bind)......然后是 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() 之前清除我的绑定(bind)。 .

编辑:

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

我让最外面的 View 处理所有绑定(bind),因此当重新加载 View (也就是表格中的单元格)时,绑定(bind)仍然存在,因为最外面的 View 没有被释放。所以,我所有的 subview 都没有处理它们自己的绑定(bind),这是一个重大错误。

为了解决这个问题(以我想象的“正确”方式),我让我的自定义单元格继承自 MvxTableViewCell 并添加了延迟绑定(bind)。
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[]ObservableCollectionWeekViewModel
就像我说的,长话短说,我不需要使用 ClearBindings(view)

最佳答案

How does the ClearBindings() work in MvvmCross?


ClearBindings()ClearBindings(view)
每个MvxBindingContext维护 3 个单独的“绑定(bind)”列表:
  • 直接在上下文中创建的绑定(bind)列表
  • 在 subview 中创建的基于 View 的绑定(bind)查找表 - 这些当前仅在 subview 在父绑定(bind)上下文中动态膨胀时用于 Android 绑定(bind)。
  • 等待第一次调用 DataContext=value 的操作列表(通常会继续创建绑定(bind))

  • 其中第一个是目前在 iOS 中使用的主要 API,也是唯一公开的、MvxBindingContext 的 API。该列表的暴露是ClearAll .

    第二个仅在 Android 中用于某些 subview Xml 膨胀 - 和 ClearBindings(view)允许这些用于此目的。

    所有这些背后的历史都与许多 Android 和 iOS 内存管理问题密切相关——尤其是试图确保我们跟踪和处理所有绑定(bind),包括在 subview 、列表等中创建的绑定(bind)。

    如果我们可以在这里挖掘出一个好的用例——从“这只是为了一些测试目的”稍微扩展一下——然后是 ClearBindingsForObjectEnumerateBinding API 可以考虑用于扩展的绑定(bind)上下文 API - 但它肯定需要更严格的要求来确保项目捕获 API 真正有用的内容。

    与此同时,我想如果你愿意的话,我想你可以使用 tableView 作为查找来创建和注册绑定(bind) - 例如就像是:
    var bindings = MvxBindingSingletonCache.Instance.Binder.Bind(BindingContext.DataContext, tableView, "ItemsSource MySource");
    this.RegisterBindingsFor(tableView, bindings);
    

    这将使您能够调用ClearBindings(tableView);
    或者,如果您想阻止单个绑定(bind)工作,那么您可以 Dispose早点 - 这将清除它的源绑定(bind) - 例如如果你这样做了:
        _myDisposableBindings = MvxBindingSingletonCache.Instance.Binder.Bind(BindingContext.DataContext, tableView, "ItemsSource MySource");
        this.AddBindings(_myDisposableBindings);
    

    那么你可以在以后的某个时候做类似的事情:
        foreach (var binding in _myDisposableBindings)
        {
            binding.Dispose();
        }
        _myDisposableBindings = null;
    

    或者-也许这就是我要走的路(尽管这取决于您的用例到底是什么)-然后将表放在自己的MvxView中可能会更容易控制 - 可以拥有自己的 BindingContext您可以调用ClearAllBindings()在。

    更多关于 MvxViewhttp://slodge.blogspot.co.uk/2013/06/n32-truth-about-viewmodels-starring.html

    最后,我可能还会考虑确定您是否可以只保留绑定(bind),但可以清除 ItemsSource在您的 View 模型中。

    关于xamarin.ios - MVVMCross:ClearBindings() - 如何在 Touch 中使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17680846/

    相关文章:

    android - MVVMCross 绑定(bind) - 如何更改对象的可见性

    windows - 通过ViewModel自定义UWP ListViewItem

    ios - mvvmcross MvxActionBasedTableViewSource 作为分组表

    ios - 无法加载 plist 文件,错误无法解析 Plist 数据类型 :key

    c# - 如何使用 Monotouch.Dialog 设置背景图片

    c# - Zxing 的 System.Drawing.Bitmap 中可能存在内存泄漏

    android - MvvmCross 和 Xamarin Forms,Android 应用程序在获取强调色时因空引用错误而崩溃

    c# - ZXing Barcode ImageView不显示生成的条形码Xamarin Forms

    keyboard - 我可以通过触摸 DialogViewController (MonoTouch.Dialog) 的背景来关闭 iPhone 键盘吗?

    android - 使用 MvxExpandableListView