【问题标题】:MonoTouch.Dialog: How to access parent DialogViewController from within Element? Difference between Tapped event and Selected() method?MonoTouch.Dialog:如何从 Element 中访问父 DialogViewController? Tapped 事件和 Selected() 方法之间的区别?
【发布时间】:2012-10-17 11:46:26
【问题描述】:
我将StringElement 子类化为MonoTouch.Dialog。
在那里我可以附加到Tapped 事件,或者我可以覆盖Selected()。
如果我点击元素,两者都会触发。
但是,Selected() 允许我访问该元素所属的 DialogViewController,此信息不会传递给 Tapped 事件。
这里的逻辑是什么?一个元素是否应该知道它的DialogViewController?如果是:那么如何从Tapped 事件中获取控制器?
【问题讨论】:
标签:
xamarin.ios
monotouch.dialog
【解决方案1】:
通过查看 Github 上的源代码找到了自己。
触发Tapped 事件的唯一地方是来自Selected()。所以我认为 Tapped 应该是 EventHandler 类型而不是 Action。
public override void Selected (DialogViewController dvc, UITableView tableView, NSIndexPath indexPath)
{
if (Tapped != null)
Tapped ();
tableView.DeselectRow (indexPath, true);
}
【解决方案2】:
在我编写该代码时,想法很简单,即使用 lambda,您可以将所需的任何状态传递给您的 Tapped 处理程序,而无需使用 object/EventArgs 模式。
所以你会做这样的事情:
var dialogViewController = CreateDvC ();
new StringElement ("....", () => {
// reference any variables here
// my container is:
Console.Writeline (dialogViewController);
}