Xamarin.Android button click event is not registering new click handler

So I have this button which I set a click handler to.However using a conditional I want to set a new click handler to that same button, but when I click it it still uses the old click handler. How do I fix this? I did set a break point on both click handlers to make sure and no matter what it still uses the same click handler(old one or first one set). I'm guessing this is happening because the view is never resetting just changing the content and as such it keeps the old click event bound and the new one never gets set.

Here is my code:

`public void loadPage(int pageId) {
mCurrentPage = mStory.getPage (pageId);
Drawable drawable = Resources.GetDrawable (mCurrentPage.getPageImageId ());
storyImage.SetImageDrawable (drawable);
string text = String.Format (mCurrentPage.getStoryText (), name);
storyText.Text = text;

        if (mCurrentPage.isFinal()) {
            Console.WriteLine ("Nade it");
            choice1.Visibility = ViewStates.Invisible;
            choice2.Click += delegate {
                Finish ();
            };

        } else {
            choice1.Text = mCurrentPage.getChoice1 ().getText ();
            choice2.Text = mCurrentPage.getChoice2 ().getText ();

            if (!choice1.HasOnClickListeners && !choice2.HasOnClickListeners) {
                choice1.Click += (sender, e) => {
                    int nextPageId = mCurrentPage.getChoice1 ().getNextPageId ();
                    loadPage (nextPageId);
                };

                choice2.Click += delegate {
                    int nextPageId = mCurrentPage.getChoice2().getNextPageId();
                    loadPage(nextPageId);
                };
            }
        }
    }`

Posts

  • ashalvaashalva GEMember ✭✭✭
    edited November 2015

    Hi @John_C,

    First of all try to subscribe on button click and create separate method for your handler. Next you must unsubscribe from MyChoice and subscribe for another:

    public void loadPage(int pageId) { choice1.Click += MyChoice; if(mCurrentPage.isFinal()) { choice1.Click -= MyChoice; choice1.Click += FinalChoice; } } private void FinalChoice(object sender, EventArgs e){ Finish(); } private void MyChoice(object sender, EventArgs e){ //your logic here }

  • John_CJohn_C USMember

    Thanks @ashalva this worked like a charm :) gonna research sending parameters in now since MyChoice pretty much would fit both buttons. Thanks!

Sign In or Register to comment.