5

现在在我的一些项目中使用很棒的 MonoTouch.dialog 并且有一个问题。我有一个 RadioGroup 用于允许用户选择他的家庭状态,States 是一个字符串数组。

    public static RootElement CreateStates ()
    {
        return new RootElement ("State", new RadioGroup (0)) 
        {
            new Section ("Choose State")
            {
                from x in States
                   select (Element) new RadioElement (x) 
            }
        };
    }

这很好用,当我选择状态时,会出现弹出窗口并选择我的状态,但是我必须点击导航栏中的后退按钮才能回到我的主屏幕。当我选择一个选项时,有没有办法让该弹出窗口消失?不得不按后退按钮很烦人。还是我只是完全使用了错误的解决方案?

我的第一个想法是继承 RadioElement 并捕获选定的事件,但是我仍然不确定如何关闭自动选择弹出窗口?

4

1 回答 1

13

今天早上我遇到了同样的问题,我还想触发一个更改事件,以便在编辑数据时在对话框上添加一个“取消”按钮。这两个任务都要求您继承 RadioElement 并覆盖 Selected 方法 - 请注意额外的步骤,以确保如果用户单击已选择的项目,对话框不会关闭 - 如果您单击任何内容,即使它已经被选中,它也会触发,所以我想防止这种情况发生-我的看起来像这样。

public class MyRadioElement : RadioElement {
    // Pass the caption through to the base constructor.
    public MyRadioElement (string pCaption) : base(pCaption) {
    }

    // Fire an event when the selection changes.
    // I use this to flip a "dirty flag" further up stream.
    public override void Selected (
        DialogViewController pDialogViewController, 
        UITableView pTableView, NSIndexPath pIndexPath) {
        // Checking to see if the current cell is already "checked"
        // prevent the event from firing if the item is already selected.  
        if (GetActiveCell().Accessory.ToString().Equals(
            "Checkmark",StringComparison.InvariantCultureIgnoreCase)) {
            return;
        }

        base.Selected (pDialogViewController, pTableView, pIndexPath);

        // Is there an event mapped to our OnSelected event handler?
        var selected = OnSelected;

        // If yes, fire it.
        if (selected != null) {
            selected (this, EventArgs.Empty);
        }

        // Close the dialog.
        pDialogViewController.DeactivateController(true);
    }

    static public event EventHandler<EventArgs> OnSelected;
}
于 2011-12-22T17:11:16.810 回答