Monotouch.Dialog: After selection in a RadioElemen

2019-08-31 18:10发布

问题:

I am developing a Unit Converter using Monotouch.Dialog, and I have a main window that looks like this:

When I tap the Quantity element, select a new Quantity and return to the main window, I want to update the Unit elements with a table of units associated with the selected quantity.

My initial thought was to make an update of the units in an event handler that would be invoked after completed quantity selection. However, I have not been able to find an event in the MT.D API that is fired when I return from the quantity selection. Is there such an event that I could act upon, or is there another way I can do the unit updating?

Schematically, my AppDelegate.FinishedLaunching method looks like this right now:

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

  _quantityGroup = new RadioGroup("Qty", 0);
  _fromUnitGroup = new RadioGroup("FromU", 0);
  _toUnitGroup = new RadioGroup("ToU", 0);

  var quantityElem = new RootElement("Quantity", _quantityGroup) { 
      new Section() { new RadioElement("AbsorbedDose", "Qty") as Element, ... }};

  var fromUnitElem = new RootElement("Unit", _fromUnitGroup) { new Section() };
  var toUnitElem = new RootElement("Unit", _toUnitGroup) { new Section() }

  _rootElement = new RootElement ("Unit Converter")
  {
    new Section() { quantityElem }, 
    new Section("From") { new EntryElement(...), fromUnitElem },
    new Section("To") { new EntryElement(...), toUnitElem }
  };

  _rootVC = new DialogViewController(_rootElement);
  _nav = new UINavigationController(_rootVC);

  _window.RootViewController = _nav;
  _window.MakeKeyAndVisible();

  return true;
}

回答1:

As pointed out by Jason in a comment above this problem had been addressed previously on SO, here.

The solution is to extend the RadioElement class, where the inheriting class contains an OnSelected event and the RadioElement.Selected method is overridden with a method that invokes the OnSelected event handler(s).