Using DecelerationEnded interferes with other callbacks
Asked Answered
S

1

2

I'm trying to use the DecelerationEnded callback in conjunction with 'Tapped' callbacks on MT.Dialog elements. I can't get the two to work at the same time.

When the DecelerationEnded callback is commented out, the 'tapped' callbacks work. When it's commented in, the 'tapped' callbacks don't get triggered anymore (whereas DecelerationEnded does).

When the DecelerationEnded call is moved above the setting of the Root, then the button 'tapped' callbacks work, but the DecelerationEnded callback doesn't. Delaying the callback setup until ViewWillAppear also doesn't fix anything.

Any solutions?

Example code:

public class TestController : DialogViewController
{
    public TestController () : base(UITableViewStyle.Plain, null, true)
    {
        // Create list of 20 buttons.
        Section s = new Section();

        for (int i = 0; i < 20; i++ )
        {
            s.Add(new StringElement("test " + i, () => {
                Console.WriteLine("Tapped"); // Tapped callback.
            }));
        }

        Root = new RootElement("Test") {s};

        // The following line causes all the "tapped" handlers to not work.
        this.TableView.DecelerationEnded += HandleDecelerationEnded;
    }

    void HandleDecelerationEnded (object sender, EventArgs e)
    {
        Console.WriteLine ("Deceleration Ended");
    }
}
Spore answered 31/5, 2012 at 10:42 Comment(4)
I know this doesn't help, but I got the same strange behavior when I created a test project for this issue. I also tried "var dvc = new DialogViewController()" instead of subclassing it, but got the same thing. I'll play with it a little more...Joyance
Thanks for looking into it. I've tried every variation I can think of to fix this, with no luck. DecelerationEnded seems to override other callbacks as well (like DialogViewController.OnSelection).Spore
I've downloaded the MT.D source code and will go through it later to see if I can find the problem. From what I can see so far, adding any event handler on the TableView interferes with the Selected event for the StringElement.Joyance
Thanks for the help Mike. Have you been able to dig up anything?Spore
N
2

In MonoTouch you can use either the C# style of callbacks or the Objective-C style of callbacks, but they can not be mixed together:

http://docs.xamarin.com/ios/advanced_topics/api_design#Delegates

Internally the MonoTouch.Dialog library implements its features by providing a full subclass that handles all of the events. If you use the C# syntax, it replaces the built-in handler with a proxy that in this case, merely responds to DecelerationEnded.

If you want to hook up to this, you need to subclass the existing "Source" class, and create this by overriding the CreateSizingSource, and it should provide a new instance of your class. This is the virtual method that you need to override to provide the same behavior but with your own classes:

public virtual Source CreateSizingSource (bool unevenRows)
{
    return unevenRows ? new SizingSource (this) : new Source (this);
}

You can just subclass SizingSource and override the method there for the deceleration method.

TweetStation has a sample that shows how this is done: it uses the same event to determine when to remove the number of unread tweets from the screen.

Northumberland answered 1/6, 2012 at 13:37 Comment(2)
Thank you, that makes a bit more sense. I've edited the answer to include a full example solution.Spore
Thanks so much for taking the time to answer, Miguel. I was really scratching my head on that one! I'm still climbing that learning curve...Joyance

© 2022 - 2024 — McMap. All rights reserved.