我想知道除了这个之外是否有简单明了的方法从嵌套的listview控制器访问主对象。
((PropertyCollectionSource)((ListView)View).CollectionSource).MasterObject
我是否必须在需要访问主对象的任何地方写这个?
我认为这不是一种优雅的方式,看起来很蹩脚。
答案 0 :(得分:3)
尚未测试,但您可以使用以下ViewController后代:
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;
}
}
}
答案 1 :(得分:1)
用C#6简化上述答案
public partial class NestedViewController : ViewController
{
protected PropertyCollectionSource PropertyCollectionSource => (View as ListView)?.CollectionSource as PropertyCollectionSource;
protected object MasterObject => PropertyCollectionSource?.MasterObject;
}
我也把它移到了一个函数
public static class HandyControllerFunctions
{
public static object GetMasterObject(View view)
{
var propertyCollectionSource = (view as ListView)?.CollectionSource as PropertyCollectionSource;
return propertyCollectionSource?.MasterObject ;
}
}
并将其称为例如
var myObject = HandyControllerFunctions.GetMasterObject(View)as IMyObject