xamarin
  1. xamarin-form-buttons

Xamarin Forms Buttons

Buttons are an essential user interface element that allows users to take action within a mobile application. Xamarin.Forms provides an easy way to create cross-platform buttons that are fully customizable. In this tutorial, we will learn how to create button controls in Xamarin.Forms.

Syntax

<Button Text="ButtonText" Clicked="EventHandlerMethod"/>
  • Text: The text property sets the initial text of the button control.
  • Clicked: The event handler executed when the button is clicked.

Example

<Button Text="Click Me" Clicked="OnButtonClick"></Button>

Output

When the above button is clicked, the OnButtonClick method is triggered, and the button control's text is changed to "Clicked."

Explanation

In the example above, we use the <Button> tag to create a button control. We set the initial text of the button control using the Text property. We also assign the OnButtonClick method as the event handler when the button is clicked.

private void OnButtonClick(object sender, EventArgs e)
{
    (sender as Button).Text = "Clicked";
}

In the OnButtonClick method, we cast the sender object as a Button control, update the control's text to "Clicked".

Use

Buttons can be used in various scenarios such as triggering an action, submitting a form, navigating to another page, and so on. Buttons can also be customized by changing their background color, text color, font, and other properties.

Important Points

  • Button controls can be used to perform various actions within a mobile application.
  • In Xamarin.Forms, the <Button> tag is used to create button controls.
  • The Clicked event is triggered when the button control is clicked.
  • Button properties can be customized to fit your application's theme and design.

Summary

Buttons are an essential user interface element that allows users to interact with a mobile application. Xamarin.Forms provides an easy way to create cross-platform buttons that are fully customizable. In this tutorial, we learned how to create button controls using Xamarin.Forms, assign event handlers, and customize button properties.

Published on: