52 Weeks of Xamarin: Week 3- Events in Xamarin.Forms

In this, week 3 of 52 Weeks of Xamarin, we’re going to pick up where we left off last week, and add event handling to our buttons

OnSaveClicked2

There is much to do to make this a working program, and starting next week we’ll begin to add real logic, as well as a Model class (data) and a ViewModel class (logic).  We’ll also begin to add unit tests, and shoot for test-driven development.

But I did promise to illustrate how event handling works, and so this week I’ll show how to respond to a button press.  This is very simple, which means this week’s posting will be a bit short.

In the XAML posted last week, there are three buttons:

 <Button
   BackgroundColor="Green"
   TextColor="White"
   WidthRequest="75"
   Text="Save" />

 <Button
   BackgroundColor="Red"
   WidthRequest="75"
   TextColor="White"
   Text="Cancel" />

 <Button
   BackgroundColor="Blue"
   WidthRequest="75"
   TextColor="White"
   Text="Review" /> > 

Each of these buttons needs to identify an event handler for their click event.  The easiest way to do so is in the XAML:

 <Button
   BackgroundColor="Green"
   TextColor="White"
   WidthRequest="75"
   Clicked="OnSaveClicked"
   Text="Save" />

 <Button
   BackgroundColor="Red"
   WidthRequest="75"
   TextColor="White"
   Clicked="OnCancelClicked"
   Text="Cancel" />

 <Button
   BackgroundColor="Blue"
   WidthRequest="75"
   TextColor="White"
   Clicked="OnReviewClicked"
   Text="Review" />

Notice the pattern.  Each button’s click event handler is assigned to the Clicked event.  It is a convention to name the event handler beginning with “On” and typically then adding an identifier (e.g., Save) and the event (e.g.,Clicked).  Thus, OnSaveClicked

In your code (for now, in your code behind file) you will implement each event handler.  Open CreatePage.xaml.cs and add the following:

 public void OnSaveClicked(object o, EventArgs e){
     DisplayAlert("Event Handler", "Save button clicked", "OK");
 }
  

Every event handler in .NET, by convention, is public, returns void and takes two arguments.  The first, of type object, is the sender of the event (in this case the button) and the second is either of type EventArgs or a type derived from EventArgs.

EventArgs (Event Arguments) has nothing in it, but derived types can have additional information useful in handling the event.

In this case, we are doing nothing more than opening a dialog box.  The first parameter is the header to the dialog box, the second is the message, and the third is the text associated with the button that closes the dialog:

OnSaveClicked

 

 

Share

About Jesse Liberty

Jesse Liberty is an independent consultant and programmer with three decades of experience writing and delivering software projects. He is the author of 2 dozen books and multiple Pluralsight courses, and has been a Senior Technical Evangelist for Microsoft, a Distinguished Software Engineer for AT&T, a VP for Information Services for Citibank and a Software Architect for PBS. He is a Xamarin Certified Mobile Developer and a Xamarin MVP, Microsoft MVP and Telerik MVP.
This entry was posted in Essentials. Bookmark the permalink.

2 Responses to 52 Weeks of Xamarin: Week 3- Events in Xamarin.Forms

  1. It is an interesting and old debate. Many of us feel that having event handling in the code-behind is fine unless it interferes with testing. In any case, I will write a post soon demonstrating how to migrate at least some of that logic to the VM.

    Thanks!

  2. Jason says:

    Jesse, I wanted to thank you for all of the Xamain.Forms posts, podcast and courses you have been churning out; great stuff!

    I do have a question regarding the handling of events and mvvm. In all of the examples and demos I’ve seen, the OnTapped/OnClicked events are always handled in the xaml page code-behind. Is this truly Kosher while striving to stay within a mvvm architecture? If not, could you point me to any examples of how to move your event handlers into the ViewModel?

    Thanks for your insight!

Leave a Reply

Your email address will not be published.