【问题标题】:MonoTouch Message Element opes page with string elementsMonoTouch Message Element 使用字符串元素操作页面
【发布时间】:2012-12-04 14:40:13
【问题描述】:

我有以下代码:

Section _section = new Section ("Test");


foreach (ExampleData data in Example.data) {

     MessageElement Item = new MessageElement (){

    Sender = data.Name,
    Subject = data.Value,
    Body = data.Description,
    Date = data.Modified
         } ;

          _section.Add(Item);


            var root = new RootElement("Item Expanded"){

                new Section ("test2"){
                    new StringElement("Field Name", data.FieldName),
                    new StringElement("Value", data.Value),
                    new StringElement("Description", data.Description)
                }


            } ;
            _section.Add(root);

        } ;



        var _rootElement = new RootElement ("Items") {
            _section
        } ;

我希望它以这样一种方式工作,当点击消息元素时,它会显示具有相同数据的 ("test2") 部分(例如,数据是在同一循环运行期间添加的。)我意识到目前不会发生这种情况,因为它似乎是消息元素 需要一个动作委托来对点击事件执行任何操作,另外我将所有内容添加到同一部分。但是,有没有办法使用消息元素复制多个嵌套根元素和部分的行为?如果我创建新页面/屏幕并尝试以这种方式进行转换,它会搁置导航控制器并且我无法使用后退按钮,即使“push”设置为 true。

【问题讨论】:

  • 您是否尝试在点击/单击事件时将该部分动态添加到根元素?此外,这不是 iOS 方式。它更喜欢“推动”而不是扩展。
  • 我在此处的示例代码中谈到了嵌套:github.com/migueldeicaza/MonoTouch.Dialog#monotouchdialog 这被认为是不好的做法吗?
  • 我想我误解了你,我仍然不清楚你想要发生什么。

标签: xamarin.ios monotouch.dialog


【解决方案1】:

不确定你到底想要什么。用这个替换你的“Item Expanded”根元素代码,用后退按钮在导航堆栈上推送一个对话框视图控制器。当然,您的 DialogViewcontroller 应该首先在 UINavigation 控制器中才能正常工作

        Item.Tapped += delegate(DialogViewController arg1, UITableView arg2, NSIndexPath arg3) 
        {
            var newDialogVC = new DialogViewController(
                UITableViewStyle.Grouped,
                new RootElement("Item Expanded")
                {
                    new Section ("test2"){
                    new StringElement("Field Name", "test"),
                    new StringElement("Value", "test"),
                    new StringElement("Description", "test")
                }
                                                }
                , true);

            arg1.NavigationController.PushViewController(newDialogVC,true);
        };

【讨论】:

  • 说得太早了。循环中的最后一个元素最终添加到每个消息元素的委托中的每个字符串元素。就像他们被传递 ByRef 而不是 ByVal
  • 您可能应该将循环更改为 for 循环,然后通过索引 Example.Data[i].FieldName 引用委托中的数据。这是因为代理仅在您点击消息时执行。那时 foreach ,oop 已经完成,数据是最后一个元素
  • 实际点击该项目时会导致索引超出范围异常。
  • 在不了解上下文和查看代码的情况下很难解决。您应该将数据数组/集合作为类变量(而不是局部变量)进行存储和引用。到委托执行时,viewdidload 已经完成。
  • 通过使用 Example.Data[arg3.Row].FieldName 得到它再次感谢!
猜你喜欢
  • 1970-01-01
  • 2012-11-18
  • 1970-01-01
  • 1970-01-01
  • 2016-06-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-15
相关资源
最近更新 更多