0

Based on the following question:

Use Mvvmcross Binding with MonoTouch.Dialog (Lists and Commands)

I have tried to implement a bindable section to allow an async call to update the radio group list based on the previous selection.

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

The list is bound with the following code

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
    }
}

The list updates and I can see the newly bound elements but when I select the list the whole application crashes.

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 

Any help would be greatly appreciated..

Community
  • 1
  • 1
Ollie
  • 1,140
  • 8
  • 26
  • There is a radio group sample in https://github.com/MvvmCross/MvvmCross-Tutorials/tree/master/DialogExamples – Stuart Feb 21 '14 at 18:03
  • Ok so this looks to be the same issue that I posted in [this SO post](http://stackoverflow.com/questions/23695850/mvvmcross-bindable-monotouch-dialog-radioelement) - could you perhaps share your solution? I can't find the radio group sample @Stuart is referring to. – mrtnkrstn May 20 '14 at 17:45

0 Answers0