2

I want to know if there is simple and clear method other than this for accessing master object from a nested listview controller.

((PropertyCollectionSource)((ListView)View).CollectionSource).MasterObject

Do I have to write this in everywhere where i need to access master object?

I think this is not an elegant way and looks so lame.

Sayse
  • 42,633
  • 14
  • 77
  • 146
Onur
  • 852
  • 9
  • 18

2 Answers2

3

Not tested yet, but you can use the following ViewController descendant:

public class NestedViewController : ViewController
{
    protected PropertyCollectionSource PropertyCollectionSource
    {
        get
        {
            return View is ListView ? ((ListView)View).CollectionSource is PropertyCollectionSource ? ((ListView)View).CollectionSource as PropertyCollectionSource : null : null;
        }
    }

    protected object MasterObject
    {
        get
        {
            return PropertyCollectionSource != null ? PropertyCollectionSource.MasterObject : null;
        }
    }
}
ErikWitkowski
  • 460
  • 3
  • 8
1

Simplifying the above answer with C#6

public partial class NestedViewController : ViewController
{
    protected PropertyCollectionSource PropertyCollectionSource => (View as ListView)?.CollectionSource as PropertyCollectionSource;

    protected object MasterObject => PropertyCollectionSource?.MasterObject;
}

Also I moved it into a function

public static class HandyControllerFunctions
{
    public static object GetMasterObject(View view)
    {
        var propertyCollectionSource = (view as ListView)?.CollectionSource as PropertyCollectionSource;
        return propertyCollectionSource?.MasterObject ;
    }
}

and call it for example

var myObject = HandyControllerFunctions.GetMasterObject(View) as IMyObject

Kirsten
  • 15,730
  • 41
  • 179
  • 318