6 Replies - 684 Views - Last Post: 06 May 2017 - 09:21 AM

#1 Dialupp  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 210
  • Joined: 31-October 16

Why does this event not get fired?

Posted 05 May 2017 - 05:32 PM

Hey guys so I am working through a sample in this book and doesn't fire this event when I press the "remove" button, that's because I remove this code here: removeButton.Clicked += OnButtonclicked;

But I don't get why that is needed to detect the remove button we already have one here "addButton.Clicked += OnButtonclicked;" and its method says what to do if the button is not the add button:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
namespace YoutubeApp
{
    class TwoButtonsPage : ContentPage
    {

        Button addButton, removeButton;
        StackLayout loggerLayout = new StackLayout();
        public TwoButtonsPage()
        {
            // Create the Button views and attach Clicked handlers.
            addButton = new Button
            {
                Text = "Add",
                HorizontalOptions = LayoutOptions.CenterAndExpand
            };
            addButton.Clicked += OnButtonclicked;
            removeButton = new Button
            {
                Text = "Remove",
                HorizontalOptions = LayoutOptions.CenterAndExpand,
                IsEnabled = false
            };
            removeButton.Clicked += OnButtonclicked;
            this.Padding =
            new Thickness(5, Device.OnPlatform(20, 0, 0), 5, 0);
            // Assemble the page.
            this.Content = new StackLayout
            {
                Children =
            {
                    new StackLayout
                    {
                        Orientation = StackOrientation.Horizontal,
                        Children = {
                            addButton,removeButton

                        }
                    },
                    new ScrollView
                    {
                        VerticalOptions = LayoutOptions.FillAndExpand,
                        Content = loggerLayout
                    }



    }
            };

        }

        void OnButtonclicked(object sender, EventArgs args)
        {
            Button button = (Button)sender;
            if (button == addButton)
            {
                // Add Label to scrollable StackLayout.
                loggerLayout.Children.Add(
            new Label
            {
                Text = "Button clicked at " + DateTime.Now.ToString("T")
            });
            }
            else
            {
                // Remove topmost Label from StackLayout.
                loggerLayout.Children.
                RemoveAt(0);
            }
            // Enable "Remove" button only if children are present.
            removeButton.IsEnabled = loggerLayout.Children.Count > 0;
        }



    }
}



This post has been edited by Dialupp: 05 May 2017 - 05:33 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Why does this event not get fired?

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13482
  • View blog
  • Posts: 53,819
  • Joined: 12-June 08

Re: Why does this event not get fired?

Posted 06 May 2017 - 07:18 AM

'addbutton' and 'removebutton' are two different objects. If the example wants both to trigger the same event method then both need to have their 'Clicked' event pointed to it. Just because you change one doesn't mean the other knows what's up.
Was This Post Helpful? 0
  • +
  • -

#3 Dialupp  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 210
  • Joined: 31-October 16

Re: Why does this event not get fired?

Posted 06 May 2017 - 08:30 AM

Yeah I get they are 2 different objects but I am saying is if that one event handler will execute that method and the if statement will do "loggerLayout.Children.Add(" then that indicates that was the "addbutton" else its the "removeButton" what am I missing?
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13482
  • View blog
  • Posts: 53,819
  • Joined: 12-June 08

Re: Why does this event not get fired?

Posted 06 May 2017 - 08:31 AM

I do not understand what you wrote.
Was This Post Helpful? 0
  • +
  • -

#5 Dialupp  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 210
  • Joined: 31-October 16

Re: Why does this event not get fired?

Posted 06 May 2017 - 08:44 AM

Yeah sorry thought it might not make sense. Hard to explain so basically the OnButtonclicked method pretty much detects what button is clicked so if its add button do this else do this. Why do we need 2 calls to the methods i.e the eventhandler? When we can just have one?
Was This Post Helpful? 0
  • +
  • -

#6 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13482
  • View blog
  • Posts: 53,819
  • Joined: 12-June 08

Re: Why does this event not get fired?

Posted 06 May 2017 - 08:51 AM

Again... two different objects. If the event method does two different things based on which button is pressed, and you only assign the event to trigger on one button then why have the other 'else'? You very well could have had two event methods.. one for each button and assigned their onclick events to go to different methods.. this example does it differently and has both point to the same method.
Was This Post Helpful? 0
  • +
  • -

#7 Dialupp  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 210
  • Joined: 31-October 16

Re: Why does this event not get fired?

Posted 06 May 2017 - 09:21 AM

Ah ok might take a bit of reading thanks.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1