基于以下问题:
Use Mvvmcross Binding with MonoTouch.Dialog (Lists and Commands)
我尝试实现一个可绑定部分,允许异步调用根据之前的选择更新无线电组列表。
public class BindableLocalitySection : Section
{
private IEnumerable _itemsSource;
private MvxNotifyCollectionChangedEventSubscription _subscription;
public BindableLocalitySection()
: base()
{
}
public BindableLocalitySection(string caption)
: base(caption)
{
}
[MvxSetToNullAfterBinding]
public IEnumerable ItemsSource
{
get { return _itemsSource; }
set { SetItemsSource(value); }
}
protected virtual void SetItemsSource(IEnumerable value)
{
if (_itemsSource == value)
return;
if (_subscription != null)
{
_subscription.Dispose();
_subscription = null;
};
_itemsSource = value;
if (_itemsSource != null && !(_itemsSource is IList))
MvxBindingTrace.Trace(MvxTraceLevel.Warning,
"Binding to IEnumerable rather than IList - this can be inefficient, especially for large lists");
var newObservable = _itemsSource as INotifyCollectionChanged;
if (newObservable != null)
_subscription = newObservable.WeakSubscribe(OnItemsSourceCollectionChanged);
NotifyDataSetChanged();
}
private void OnItemsSourceCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
NotifyDataSetChanged();
}
private void NotifyDataSetChanged()
{
var newElements = new List<Element>();
if (_itemsSource != null)
{
foreach (var item in _itemsSource)
{
var a = (BaseSearchViewModel.Locality)item;
var element = new RadioElement(a.Name);
newElements.Add((Element) element);
}
}
Elements.Clear();
Elements.AddRange(newElements);
var root = this.Parent as RootElement;
if (root == null)
{
root = this.GetImmediateRootElement();
}
if (root != null && root.TableView != null)
{
root.TableView.ReloadData();
}
}
}
该列表与以下代码绑定
var bindableLocalitySection = new BindableLocalitySection();
bindableLocalitySection.Bind(bindings, element => element.ItemsSource, vm => vm.Localities);
var locality = new RootElement("Locality", new RadioGroup("Locality", 0))
{
bindableLocalitySection
};
Root = new RootElement("Advanced Search")
{
new Section("Locality")
{
locality as Element
}
}
列表更新,我可以看到新绑定的元素,但是当我选择列表时,整个应用程序崩溃了。
Object reference not set to an instance of an object
at CrossUI.Touch.Dialog.Elements.RadioElement.SubscribeToRoot () [0x00000] in <filename unknown>:0
at CrossUI.Touch.Dialog.Elements.RadioElement.GetCellImpl (MonoTouch.UIKit.UITableView tv) [0x00000] in <filename unknown>:0
at CrossUI.Touch.Dialog.Elements.Element.GetCell (MonoTouch.UIKit.UITableView tv) [0x00000] in <filename unknown>:0
at CrossUI.Touch.Dialog.DialogViewController+Source.GetCell (MonoTouch.UIKit.UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath) [0x00000] in <filename unknown>:0
at (wrapper managed-to-native) MonoTouch.UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr)
at MonoTouch.UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x0004c] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:38
at <<Removed Name>>.Main (System.String[] args) [0x00008] in <<Removed Path>>Main.cs:17
任何帮助将不胜感激..