Button click event not triggering

Hello,

I've only written desktop applications so far and now want to get started with Android, but I'm having some problems getting started.

My current problem is that the button click event is not triggered, but the referencing of the button itself is correct according to the debugger.

Thank you.

Sincerely,
Chris

using System;
using Android.OS;
using Android.Support.V4.App;
using Android.Views;
using Android.Widget;

namespace App1.Fragments
{
    public class Fragment1 : Fragment
    {
        /**
         * The initializer
         */
        public static Fragment1 NewInstance()
        {
            var frag1 = new Fragment1 {Arguments = new Bundle()};
            return frag1;
        }

        /**
         * This method is responsible for creating the view
         */
        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            base.OnCreateView(inflater, container, savedInstanceState);

            // Find "test" button by identifier and add click event
            var view = inflater.Inflate(Resource.Layout.fragment1, container, false);
            var button = view.FindViewById<Button>(Resource.Id.test);
            button.Click += (sender, args) => { Console.Write("Button clicked"); };

            return view;
        }
    }
}
Tagged:

Best Answer

  • HowdyHoHowdyHo US ✭✭
    Accepted Answer

    Do -

    Method I

    button.Click += delegate
    {
    Console.WriteLine("Button clicked");
    };

    Method II -

    button.Click += Hit TAB Twice -
    You will get - button.Click += Button_Click;

    private void Button_Click(object sender, EventArgs e)
    {
    Console.WriteLine("Button clicked");
    }

    Your problem is here - button.Click += (sender, args) => { Console.Write("Button clicked"); };

    Not sure exactly what though.

Answers

  • HowdyHoHowdyHo USMember ✭✭
    Accepted Answer

    Do -

    Method I

    button.Click += delegate
    {
    Console.WriteLine("Button clicked");
    };

    Method II -

    button.Click += Hit TAB Twice -
    You will get - button.Click += Button_Click;

    private void Button_Click(object sender, EventArgs e)
    {
    Console.WriteLine("Button clicked");
    }

    Your problem is here - button.Click += (sender, args) => { Console.Write("Button clicked"); };

    Not sure exactly what though.

  • LeVenceLeVence Member

    Thanks, method number one worked for me!

Sign In or Register to comment.