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

 

 

About Jesse Liberty

Jesse Liberty has three decades of experience writing and delivering software projects and is the author of 2 dozen books and a couple dozen online courses. His latest book, Building APIs with .NET will be released early in 2025. Liberty is a Senior SW Engineer for CNH and he was 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 Microsoft MVP.
This entry was posted in Essentials. Bookmark the permalink.

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

Comments are closed.