2

笔记:

有很多代码/诊断,首先我讨论我的问题,然后是后面的很多代码(帮助回答任何问题),最后我添加了 3 个不同的诊断块,希望能回答更多问题。显然,我不希望读者通读所有内容。

问题:

每当我运行我的应用程序并显示相同的视图(在关闭它之后)时,我的控制台输出都会显示以前的绑定仍然存在。

细节:

我正在显示的视图如下:有一个表视图,它的源绑定到我的 ViewModel,每个单元格都有绑定到 ViewModel 的左/右标签。

我将上述视图创建为模态弹出视图。每个单元格都将用户导航到一个新视图,他/她可以在其中编辑该字段。转到新视图并使用“后退”按钮导航不会导致任何问题(据我所知)。一旦用户按下按钮关闭模式弹出框(例如“取消”),下次显示视图时,旧绑定似乎存在。查看“诊断”会显示这些值是如何重复的。我不知道为什么。

我创建了我的表格视图,并为绑定有问题的单元格创建GetOrCreateCellFor了一个新的。TimesheetOptionCell

代码背后:

ViewDidLoad()在我的里面我TimesheetEntryView设置了ItemsSource

var source = new TimesheetTableSource (TableView, this);

var set = this.CreateBindingSet<TimesheetEntryView, TimesheetEntryViewModel> ();
set.Bind (source).For (src => src.ItemsSource).To (vm => vm.Options);
set.Apply ();
TableView.Source = source;

在我的里面TimesheetTableSource

ObservableCollection<ViewModelBase> _options;
public override System.Collections.IEnumerable ItemsSource 
{
    get 
    {
        return _options;
    }
    set 
    {
        _options = (ObservableCollection<ViewModelBase>)value;
        if (_options != null) 
        {
            this.ReloadTableData ();
        }
    }
}

public override int NumberOfSections (UITableView tableView)
{
    return 3;
}

public override int RowsInSection (UITableView tableview, int section)
{
    if (section == 0) 
    {
        return _options.Count;
    }
    else if (section == 1)
    {
        return 2;
    }
    else if (section == 2)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

protected override UITableViewCell GetOrCreateCellFor (UITableView tableView, NSIndexPath indexPath, object item)
{
    if(indexPath.Section == 0)
    {
        return TimesheetOptionCell.Create ();
    }

    GenericOneLabelCell cell = GenericOneLabelCell.Create ();

    if (indexPath.Section == 1 && indexPath.Row == 0) 
    {
        cell.LabelText = "Save";
        var set = _timesheetView.CreateBindingSet<TimesheetEntryView, TimesheetEntryViewModel> ();
        set.Bind (cell).For (cll => cll.CellEnabled).To (vm => vm.SaveEnabled);
        set.Apply ();
    } 
    else if (indexPath.Section == 1 && indexPath.Row == 1) 
    {
        cell.LabelText = "Apply";
        var set = _timesheetView.CreateBindingSet<TimesheetEntryView, TimesheetEntryViewModel> ();
        set.Bind (cell).For (cll => cll.CellEnabled).To (vm => vm.SaveEnabled);
        set.Apply ();
    }

    return cell;
}

在我的里面TimesheetOptionCell

    public TimesheetOptionCell (IntPtr handle) : base (handle)
    {
        this.DelayBind (() => 
        {
            //set.bind(<label/textView>) implicitly adds .For("Text")
            var set = this.CreateBindingSet<TimesheetOptionCell, OptionViewModelBase>();
            set.Bind(FieldDescriptionTextView).To(option => option.OptionValue);
            set.Bind(FieldTitleLabel).To(option => option.Title);
            set.Apply();
        });
    }

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

诊断:

这是我第一次显示视图:

2013-08-06 09:07:13.824 FCXiOSv2[16478:21e03] mvx: Diagnostic:   6.70 Showing ViewModel TimesheetEntryViewModel
2013-08-06 09:07:13.825 FCXiOSv2[16478:21e03] TouchNavigation: Diagnostic:   6.70 Navigate requested
2013-08-06 09:07:13.840 FCXiOSv2[16478:21e03] MvxBind: Diagnostic:   6.72 Receiving setValue to System.Collections.ObjectModel.ObservableCollection`1[FCX.Core.ViewModels.ViewModelBase]
2013-08-06 09:07:13.873 FCXiOSv2[16478:21e03] MvxBind: Diagnostic:   6.75 Receiving setValue to 7/23/2013
2013-08-06 09:07:13.874 FCXiOSv2[16478:21e03] MvxBind: Diagnostic:   6.75 Receiving setValue to Date Worked
2013-08-06 09:07:13.881 FCXiOSv2[16478:21e03] MvxBind: Diagnostic:   6.76 Receiving setValue to 0:00
2013-08-06 09:07:13.882 FCXiOSv2[16478:21e03] MvxBind: Diagnostic:   6.76 Receiving setValue to Time Worked
2013-08-06 09:07:13.888 FCXiOSv2[16478:21e03] MvxBind: Diagnostic:   6.77 Receiving setValue to 
2013-08-06 09:07:13.889 FCXiOSv2[16478:21e03] MvxBind: Diagnostic:   6.77 Receiving setValue to Company
2013-08-06 09:07:13.894 FCXiOSv2[16478:21e03] MvxBind: Diagnostic:   6.77 Receiving setValue to 
2013-08-06 09:07:13.894 FCXiOSv2[16478:21e03] MvxBind: Diagnostic:   6.77 Receiving setValue to Description
2013-08-06 09:07:13.899 FCXiOSv2[16478:21e03] MvxBind: Diagnostic:   6.78 Receiving setValue to False
2013-08-06 09:07:13.903 FCXiOSv2[16478:21e03] MvxBind: Diagnostic:   6.78 Receiving setValue to False

第二次之后,我得到了这个:

2013-08-06 09:17:43.965 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 636.85 Receiving setValue to 
2013-08-06 09:17:43.966 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 636.85 Receiving setValue to 
2013-08-06 09:17:43.967 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 636.85 Receiving setValue to 
2013-08-06 09:17:43.967 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 636.85 Receiving setValue to 
2013-08-06 09:17:43.968 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 636.85 Receiving setValue to 
2013-08-06 09:17:43.969 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 636.85 Receiving setValue to 7/23/2013
2013-08-06 09:17:43.970 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 636.85 Receiving setValue to 7/23/2013
2013-08-06 09:17:43.971 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 636.85 Receiving setValue to 0:00
2013-08-06 09:17:43.972 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 636.85 Receiving setValue to 
2013-08-06 09:17:43.972 FCXiOSv2[16478:21e03] mvx: Diagnostic: 636.85 Showing ViewModel TimesheetEntryViewModel
2013-08-06 09:17:43.973 FCXiOSv2[16478:21e03] TouchNavigation: Diagnostic: 636.85 Navigate requested
2013-08-06 09:17:44.017 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 636.90 Receiving setValue to System.Collections.ObjectModel.ObservableCollection`1[FCX.Core.ViewModels.ViewModelBase]
2013-08-06 09:17:44.024 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 636.90 Receiving setValue to 7/23/2013
2013-08-06 09:17:44.025 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 636.90 Receiving setValue to Date Worked
2013-08-06 09:17:44.031 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 636.91 Receiving setValue to 0:00
2013-08-06 09:17:44.032 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 636.91 Receiving setValue to Time Worked
2013-08-06 09:17:44.042 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 636.92 Receiving setValue to 
2013-08-06 09:17:44.043 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 636.92 Receiving setValue to Company
2013-08-06 09:17:44.050 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 636.93 Receiving setValue to 
2013-08-06 09:17:44.050 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 636.93 Receiving setValue to Description
2013-08-06 09:17:44.053 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 636.93 Receiving setValue to False
2013-08-06 09:17:44.055 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 636.94 Receiving setValue to False

第 10 次之后:

2013-08-06 09:21:08.390 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 841.27 Receiving setValue to 
2013-08-06 09:21:08.391 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 841.27 Receiving setValue to 
2013-08-06 09:21:08.391 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 841.27 Receiving setValue to 
2013-08-06 09:21:08.391 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 841.27 Receiving setValue to 
2013-08-06 09:21:08.392 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 841.27 Receiving setValue to 
2013-08-06 09:21:08.392 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 841.27 Receiving setValue to 
2013-08-06 09:21:08.392 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 841.27 Receiving setValue to 
2013-08-06 09:21:08.393 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 841.27 Receiving setValue to 
2013-08-06 09:21:08.393 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 841.27 Receiving setValue to 
2013-08-06 09:21:08.394 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 841.27 Receiving setValue to 
2013-08-06 09:21:08.394 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 841.27 Receiving setValue to 
2013-08-06 09:21:08.395 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 841.27 Receiving setValue to 
2013-08-06 09:21:08.395 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 841.28 Receiving setValue to 
2013-08-06 09:21:08.396 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 841.28 Receiving setValue to 
2013-08-06 09:21:08.396 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 841.28 Receiving setValue to 
2013-08-06 09:21:08.397 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 841.28 Receiving setValue to 
2013-08-06 09:21:08.397 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 841.28 Receiving setValue to 
2013-08-06 09:21:08.398 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 841.28 Receiving setValue to 
2013-08-06 09:21:08.399 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 841.28 Receiving setValue to 
2013-08-06 09:21:08.399 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 841.28 Receiving setValue to 
2013-08-06 09:21:08.400 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 841.28 Receiving setValue to 
2013-08-06 09:21:08.400 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 841.28 Receiving setValue to 
2013-08-06 09:21:08.400 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 841.28 Receiving setValue to 
2013-08-06 09:21:08.400 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 841.28 Receiving setValue to 
2013-08-06 09:21:08.401 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 841.28 Receiving setValue to 
2013-08-06 09:21:08.401 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 841.28 Receiving setValue to 
2013-08-06 09:21:08.401 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 841.28 Receiving setValue to 
2013-08-06 09:21:08.402 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 841.28 Receiving setValue to 
2013-08-06 09:21:08.402 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 841.28 Receiving setValue to 
2013-08-06 09:21:08.403 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 841.28 Receiving setValue to 
2013-08-06 09:21:08.403 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 841.28 Receiving setValue to 
2013-08-06 09:21:08.404 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 841.28 Receiving setValue to 
2013-08-06 09:21:08.404 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 841.28 Receiving setValue to 
2013-08-06 09:21:08.404 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 841.28 Receiving setValue to 
2013-08-06 09:21:08.404 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 841.28 Receiving setValue to 
2013-08-06 09:21:08.405 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 841.28 Receiving setValue to 
2013-08-06 09:21:08.406 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 841.29 Receiving setValue to 
2013-08-06 09:21:08.407 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 841.29 Receiving setValue to 
2013-08-06 09:21:08.407 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 841.29 Receiving setValue to 
2013-08-06 09:21:08.408 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 841.29 Receiving setValue to 
2013-08-06 09:21:08.408 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 841.29 Receiving setValue to 
2013-08-06 09:21:08.409 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 841.29 Receiving setValue to 
2013-08-06 09:21:08.409 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 841.29 Receiving setValue to 
2013-08-06 09:21:08.410 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 841.29 Receiving setValue to 
2013-08-06 09:21:08.410 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 841.29 Receiving setValue to 
2013-08-06 09:21:08.411 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 841.29 Receiving setValue to 7/24/2013
2013-08-06 09:21:08.412 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 841.29 Receiving setValue to 7/24/2013
2013-08-06 09:21:08.412 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 841.29 Receiving setValue to 7/24/2013
2013-08-06 09:21:08.413 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 841.29 Receiving setValue to 7/24/2013
2013-08-06 09:21:08.413 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 841.29 Receiving setValue to 7/24/2013
2013-08-06 09:21:08.414 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 841.29 Receiving setValue to 7/24/2013
2013-08-06 09:21:08.414 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 841.29 Receiving setValue to 7/24/2013
2013-08-06 09:21:08.415 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 841.29 Receiving setValue to 7/24/2013
2013-08-06 09:21:08.415 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 841.30 Receiving setValue to 7/24/2013
2013-08-06 09:21:08.416 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 841.30 Receiving setValue to 7/24/2013
2013-08-06 09:21:08.416 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 841.30 Receiving setValue to 7/24/2013
2013-08-06 09:21:08.417 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 841.30 Receiving setValue to 7/24/2013
2013-08-06 09:21:08.417 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 841.30 Receiving setValue to 7/24/2013
2013-08-06 09:21:08.418 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 841.30 Receiving setValue to 7/24/2013
2013-08-06 09:21:08.418 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 841.30 Receiving setValue to 7/24/2013
2013-08-06 09:21:08.419 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 841.30 Receiving setValue to 7/24/2013
2013-08-06 09:21:08.419 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 841.30 Receiving setValue to 7/24/2013
2013-08-06 09:21:08.420 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 841.30 Receiving setValue to 7/24/2013
2013-08-06 09:21:08.421 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 841.30 Receiving setValue to 0:00
2013-08-06 09:21:08.421 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 841.30 Receiving setValue to 0:00
2013-08-06 09:21:08.422 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 841.30 Receiving setValue to 0:00
2013-08-06 09:21:08.422 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 841.30 Receiving setValue to 0:00
2013-08-06 09:21:08.423 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 841.30 Receiving setValue to 0:00
2013-08-06 09:21:08.424 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 841.30 Receiving setValue to 0:00
2013-08-06 09:21:08.424 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 841.30 Receiving setValue to 0:00
2013-08-06 09:21:08.425 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 841.30 Receiving setValue to 0:00
2013-08-06 09:21:08.425 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 841.31 Receiving setValue to 0:00
2013-08-06 09:21:08.426 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 841.31 Receiving setValue to 
2013-08-06 09:21:08.426 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 841.31 Receiving setValue to 
2013-08-06 09:21:08.427 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 841.31 Receiving setValue to 
2013-08-06 09:21:08.427 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 841.31 Receiving setValue to 
2013-08-06 09:21:08.427 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 841.31 Receiving setValue to 
2013-08-06 09:21:08.427 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 841.31 Receiving setValue to 
2013-08-06 09:21:08.428 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 841.31 Receiving setValue to 
2013-08-06 09:21:08.428 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 841.31 Receiving setValue to 
2013-08-06 09:21:08.428 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 841.31 Receiving setValue to 
2013-08-06 09:21:08.428 FCXiOSv2[16478:21e03] mvx: Diagnostic: 841.31 Showing ViewModel TimesheetEntryViewModel
2013-08-06 09:21:08.429 FCXiOSv2[16478:21e03] TouchNavigation: Diagnostic: 841.31 Navigate requested
2013-08-06 09:21:08.433 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 841.31 Receiving setValue to System.Collections.ObjectModel.ObservableCollection`1[FCX.Core.ViewModels.ViewModelBase]
2013-08-06 09:21:08.440 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 841.32 Receiving setValue to 7/24/2013
2013-08-06 09:21:08.441 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 841.32 Receiving setValue to Date Worked
2013-08-06 09:21:08.446 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 841.33 Receiving setValue to 0:00
2013-08-06 09:21:08.447 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 841.33 Receiving setValue to Time Worked
2013-08-06 09:21:08.452 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 841.33 Receiving setValue to 
2013-08-06 09:21:08.453 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 841.33 Receiving setValue to Company
2013-08-06 09:21:08.459 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 841.34 Receiving setValue to 
2013-08-06 09:21:08.460 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 841.34 Receiving setValue to Description
2013-08-06 09:21:08.462 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 841.34 Receiving setValue to False
2013-08-06 09:21:08.464 FCXiOSv2[16478:21e03] MvxBind: Diagnostic: 841.34 Receiving setValue to False

很抱歉让你不知所措。谢谢!

4

1 回答 1

0

为什么视图消失后绑定仍然存在?

视图可以多次显示——它们可以出现、消失、再次出现等等——所以绑定通常是在视图的生命周期内设置的——而不仅仅是每次出现。

每个都View将在处理时清除其绑定。究竟何时Dispose发生取决于 iOS 和 Xamarin.iOS - 这取决于 UIKit 何时决定清理内存。在模拟器中,您通常可以使用“模拟内存警告”菜单选项将其提前。

如果您希望 View 在某个时间点更早地清除绑定,那么您可以调用this.ClearAllBindings()自己 - 但在大多数情况下,这不是必需的,因为 View 和 ViewModel 通常在 View 的生命周期中成对存在。


根据您的描述,我不确定我是否完全遵循您在自定义视图演示器中或在您的视图模型位置中所做的事情 - 我不确定所有Resolve<> Result跟踪线的含义。我怀疑您可能正在重用 ViewModel 和/或 View,这可能是您的多重绑定发生的地方。

于 2013-08-07T02:06:34.797 回答