Xamarin.Forms Button Tutorial
Before attempting this tutorial, you should have successfully completed the:
- Build your first Xamarin.Forms app quickstart.
- StackLayout tutorial.
In this tutorial, you learn how to:
- Create a Xamarin.Forms
Button
in XAML. - Respond to the
Button
being tapped. - Change the appearance of the
Button
.
You will use Visual Studio 2019, or Visual Studio for Mac, to create a simple application that demonstrates how to customize a Button
. The following screenshots show the final application:
You'll also use XAML Hot Reload for Xamarin.Forms to see UI changes without rebuilding your application.
Create a button
To complete this tutorial you should have Visual Studio 2019 (latest release), with the Mobile development with .NET workload installed. In addition, you will require a paired Mac to build the tutorial application on iOS. For information about installing the Xamarin platform, see Installing Xamarin. For information about connecting Visual Studio 2019 to a Mac build host, see Pair to Mac for Xamarin.iOS development.
Launch Visual Studio, and create a new blank Xamarin.Forms app named ButtonTutorial.
Important
The C# and XAML snippets in this tutorial requires that the solution is named ButtonTutorial. Using a different name will result in build errors when you copy code from this tutorial into the solution.
For more information about the .NET Standard library that gets created, see Anatomy of a Xamarin.Forms application in the Xamarin.Forms Quickstart Deep Dive.
In Solution Explorer, in the ButtonTutorial project, double-click MainPage.xaml to open it. Then, in MainPage.xaml, remove all of the template code and replace it with the following code:
<?xml version="1.0" encoding="utf-8"?> <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="ButtonTutorial.MainPage"> <StackLayout Margin="20,35,20,20"> <Button Text="Click me" /> </StackLayout> </ContentPage>
This code declaratively defines the user interface for the page, which consists of a
Button
in aStackLayout
. TheButton.Text
property specifies the text that appears in theButton
.In the Visual Studio toolbar, press the Start button (the triangular button that resembles a Play button) to launch the application inside your chosen remote iOS simulator or Android emulator:
Note that by default a
Button
tends to occupy all the space that's allowed for it - in this case, the full width of its parent (theStackLayout
).In Visual Studio, stop the application.
Handle clicks
In MainPage.xaml, modify the
Button
declaration so that it sets a handler for theClicked
event:<Button Text="Click me" Clicked="OnButtonClicked" />
This code sets the
Clicked
event to an event handler namedOnButtonClicked
that will be created in the next step.In Solution Explorer, in the ButtonTutorial project, expand MainPage.xaml and double-click MainPage.xaml.cs to open it. Then, in MainPage.xaml.cs, add the
OnButtonClicked
event handler to the class:void OnButtonClicked(object sender, EventArgs e) { (sender as Button).Text = "Click me again!"; }
When the
Button
is tapped, theOnButtonClicked
method executes. Thesender
argument is theButton
object responsible for firing theClicked
event, and can be used to access theButton
object. This event handler updates the text displayed by theButton
.Note
Besides the
Clicked
event,Button
also definesPressed
andReleased
events. For more information, see Pressing and releasing the button in the Xamarin.Forms Button guide.In the Visual Studio toolbar, press the Start button (the triangular button that resembles a Play button) to launch the application inside your chosen remote iOS simulator or Android emulator. Click the
Button
and observe that the text it displays changes:For more information about handling button clicks, see Handling button clicks in the Xamarin.Forms Button guide.
Change appearance
In MainPage.xaml, modify the
Button
declaration to change its visual appearance:<Button Text="Click me" Clicked="OnButtonClicked" TextColor="Blue" BackgroundColor="Aqua" BorderColor="Red" BorderWidth="5" CornerRadius="5" WidthRequest="150" HeightRequest="75" />
This code sets properties that change the visual appearance of the
Button
. TheTextColor
property sets the color of theButton
text, and theBackgroundColor
property sets the color of the background to the text. TheBorderColor
property sets the color of an area surrounding theButton
, and theBorderWidth
property sets the width of the border. By default, theButton
is rectangular, but it can be given rounded corners by setting theCornerRadius
property to a suitable value. In addition, the size of theButton
is changed by setting itsWidthRequest
andHeightRequest
properties.If the application is still running, save the changes to the file and the application user interface will automatically be updated in your simulator or emulator. Otherwise, in the Visual Studio toolbar, press the Start button (the triangular button that resembles a Play button) to launch the application inside your chosen remote iOS simulator or Android emulator. Observe that the
Button
appearance has changed:In Visual Studio, stop the application.
For more information about setting
Button
appearance, see Button appearance in the Xamarin.Forms Button guide.
Congratulations!
Congratulations on completing this tutorial, where you learned how to:
- Create a Xamarin.Forms
Button
in XAML. - Respond to the
Button
being tapped. - Change the appearance of the
Button
.
Next steps
To learn more about the basics of creating mobile applications with Xamarin.Forms, continue to the Entry tutorial.
Related links
Have an issue with this section? If so, please give us some feedback so we can improve this section.