Dav*_*ani 7 c# xamarin.ios ios monotouch.dialog
我正在编写一个视图控制器,用于向我的应用添加新项目.它非常适合MonoTouch.Dialog,因为它可以通过基于表格的界面轻松完成,并且每个项目的相关属性都有一个字段.
这是我目前用于显示添加项目视图的代码(简化,但核心思想仍然存在):
Item item = new Item();
TapHandler handler = new TapHandler();
BindingContext bc = new BindingContext(handler, item, "Add Item");
DialogViewController dv = new DialogViewController(bc.Root, true);
this.NavigationController.PushViewController(dv, true);
Run Code Online (Sandbox Code Playgroud)
虽然这有效,但我更愿意,如果我可以将视图的细节封装到自己的视图控制器中,那么代码可能如下所示:
UIViewController controller = new AddItemViewController();
this.NavigationController.PushViewController(controller, true);
Run Code Online (Sandbox Code Playgroud)
但是,我无法弄清楚如何实现这一点.我认为合乎逻辑的做法是创建一个子类DialogViewController.但是,所有构造函数都DialogViewController需要a RootElement.要做到这一点,你需要首先创建BindingContext.因为在调用基础构造函数之前无法运行任何代码,所以它最终不会起作用.
我的第二个方法是实现子类UIViewController,创建DialogViewController和加入对话视图控制器使用我的子类的子this.AddChildViewController(dv)和this.View.AddSubView(dv.View).虽然这最初有效,但如果在UINavigationController中有新的视图控制器,并且单击了日期元素,则日期视图将显示为模式弹出窗口而不是导航控制器层次结构.(这是有道理的,因为DialogViewController它不是NavigationController本设计中层次结构的一部分).
从那里我被卡住了.我也找不到在示例中使用MonoTouch.Dialog的任何示例.这可能吗?或者如果不是,那么为什么写这样的代码是个坏主意有充分的理由吗?
因为在调用基础构造函数之前无法运行任何代码
这不太正确.
这不是理想的,但你可以继承DialogViewController并提供一个空RootElement实例,如下所示:
public class AddItemViewController : DialogViewController {
public AddItemViewController () : base (new RootElement ()) { }
}
Run Code Online (Sandbox Code Playgroud)
稍后您可以添加您的东西(或设置新的RootElement)Root,例如
void Populate ()
{
this.Root.Add (new Section () { ... });
}
Run Code Online (Sandbox Code Playgroud)
我找到了一种方法来做到这一点,虽然看起来有点混乱(感谢 poupou 给我这个想法)。我只是在基本构造函数调用中将 Root 设置为 null,然后直接在构造函数方法中设置它:
class NewItemViewController : DialogViewController
{
private Item _item;
public NewItemViewController(bool pushing) : base(null, pushing)
{
_item = new Item();
BindingContext bc = new BindingContext(this, _item, "Add Item");
this.Root = bc.Root;
// more setup
}
// more methods
}
Run Code Online (Sandbox Code Playgroud)
我不知道您可以更改 Root 对象,而不仅仅是访问它。
| 归档时间: |
|
| 查看次数: |
1826 次 |
| 最近记录: |