Reputation: 902
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.
Upvotes: 2
Views: 663
Reputation: 18160
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
Upvotes: 1
Reputation: 470
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;
}
}
}
Upvotes: 3