3

I am using the Reflection API for MonoTouch.Dialog. What I want to accomplish is, when the user selects an item from a list, I want the navigation controller to go back. I don't want to force the user to click an item, then click the Back button to go back.

However, when trying to use the OnTap attribute, my method doesn't get executed.

public override bool FinishedLaunching (UIApplication app, NSDictionary options)
{
    window = new UIWindow (UIScreen.MainScreen.Bounds);

    var demo = new DemoClass();
    var context = new BindingContext(this, demo, "Some Demo");
    var controller = new DialogViewController(context.Root);
    var navController = new UINavigationController(controller);

    window.RootViewController = navController;
    window.MakeKeyAndVisible ();

    return true;
}

public void EnumSelected()
{
    InvokeOnMainThread(() =>
    {
        new UIAlertView("Dialog", "Enum Selected", null, "OK", null).Show();
    });
}

DemoClass

public enum DemoEnum
{
    SomeValue,
    AnotherValue,
    YetAnotherValue
}

public class DemoClass
{
    [OnTap("EnumSelected")]
    public DemoEnum SomeEnum;   
}

I know how to navigate back with the navigation controller, but without the OnTap working, I can't get that far. Am I missing something? Can anybody see where I am going wrong?

Ryan Alford
  • 7,514
  • 6
  • 42
  • 56

1 Answers1

1

In a word, you can't.

Enum's (which results in a new RootController and a bunch of RadioElement's) can't have an OnTap set, unless you do it all by hand.

https://github.com/migueldeicaza/MonoTouch.Dialog/blob/master/MonoTouch.Dialog/Reflect.cs#L337

especially, these bits:

csection.Add (new RadioElement (ca != null ? ca.Caption : MakeCaption (fi.Name)));

element = new RootElement (caption, new RadioGroup (null, selected)) { csection };

There is no trigger added to the RadioElement. You would need to change it to auto-pop the form - which needs a new/changed RadioElement

https://gist.github.com/3569920

(I can't claim this code - it came from @escoz: https://github.com/escoz/MonoMobile.Forms )

So, if you are using the built-in MT.D, you can't do it. If you don't mind maintaining your own branch (or, submit a pull request back, which is what I need to do for a few things), then this is a fairly good way to go.

Nic Wise
  • 8,061
  • 2
  • 31
  • 30