目标:在弹出窗口中显示一个ListView,该ListView包含的是nonpersistent Objects,且这些objects包含了简单类型,引用类型和集合类型。
具体描述:点击一个action,弹出一个LIstView,该ListView包含了父亲Parent列表,点击Parent的children可以选择其child。
效果图:
1.创建Parent为nonPersistent类。注意DataSourcePropery特性和XPCollection的getter中的构造函数,还有重写了Child的ToString方法
[NonPersistent]
public class Parent : XPObject
{
private static int myId = 0;
public Parent(Session session)
: base(session)
{
Oid = myId++;
}
public string Name
{
get { return GetPropertyValue<string>("Name"); }
set { SetPropertyValue<string>("Name", value); }
}
[DataSourceProperty("Children")]
public Child MyChild
{
get { return GetPropertyValue<Child>("MyChild"); }
set { SetPropertyValue<Child>("MyChild", value); }
}
private XPCollection<Child> _children;
public XPCollection<Child> Children
{
get
{
if (_children == null)
_children = new XPCollection<Child>(Session,false);
return _children;
}
}
}
[NonPersistent]
public class Child : XPObject
{
private static int myId = 0;
public Child(Session session)
: base(session)
{
Oid = myId++;
}
public string ChildName
{
get { return GetPropertyValue<string>("ChildName"); }
set { SetPropertyValue<string>("ChildName", value); }
}
public override string ToString()
{
return ChildName;
}
}
2.建立ViewController,添加PopupWindowAction
public partial class ViewController1 : ViewController
{
public ViewController1()
{
InitializeComponent();
RegisterActions(components);
}
private void popupWindowShowAction1_CustomizePopupWindowParams(object sender, CustomizePopupWindowParamsEventArgs e)
{
IObjectSpace objectspace = ObjectSpaceInMemory.CreateNew();
CollectionSource cs = new CollectionSource(objectspace, typeof(Parent));
for (int i = 0; i < 5; i++)
{
Parent parent = objectspace.CreateObject<Parent>();
Child son = objectspace.CreateObject<Child>();
son.ChildName = "kid" + i;
Child son2 = objectspace.CreateObject<Child>();
son2.ChildName = "kid__" + i;
parent.Children.Add(son);
parent.Children.Add(son2);
parent.MyChild = son2;
parent.Name = "father" + i;
cs.Add(parent);
}
ListView view = Application.CreateListView(Application.FindListViewId(typeof(Parent)), cs, false);
view.Editor.AllowEdit = true;
e.View = view;
e.DialogController.SaveOnAccept = true;
}
private void popupWindowShowAction1_Execute(object sender, PopupWindowShowActionExecuteEventArgs e)
{
XPCollection list = (XPCollection)((ListView)e.PopupWindow.View).CollectionSource.List;
foreach (Parent it in list)
{
Console.WriteLine("------\nFather[{0}]'s son is [{1}]", it.Name, it.MyChild);
Console.WriteLine("below is [{0}]'s all children", it.Name);
foreach (Child it2 in it.Children)
{
Console.WriteLine("Name:{0}", it2.ChildName);
}
Console.WriteLine("----\n");
}
}
}
在popupWindowShowAction_Execute方法中为获取弹出窗口变量的方法。下面是输出结果:
------
Father[father0]'s son is [kid0]
below is [father0]'s all children
Name:kid0
Name:kid__0
----
------
Father[father1]'s son is [kid__1]
below is [father1]'s all children
Name:kid1
Name:kid__1
----
------
Father[father2]'s son is [kid__2]
below is [father2]'s all children
Name:kid2
Name:kid__2
----
------
Father[father3]'s son is [kid__3]
below is [father3]'s all children
Name:kid3
Name:kid__3
----
------
Father[John]'s son is [kid4]
below is [John]'s all children
Name:kid4
Name:kid__4
----