Xamarin.Forms Guides | Samples

Xamarin.Forms.Button Class

A button View that reacts to touch events.

See Also: Button

Syntax

[Xamarin.Forms.RenderWith(typeof(Xamarin.Forms.Platform._ButtonRenderer))]
public class Button : View, IButtonController, IElementConfiguration<Button>

Remarks

The following example shows a basic use:

C# Example

using System;
using Xamarin.Forms;

namespace FormsGallery
{
    class ButtonDemoPage : ContentPage
    {
        Label label;
        int clickTotal = 0;

        public ButtonDemoPage()
        {
            Label header = new Label
            {
                Text = "Button",
                Font = Font.BoldSystemFontOfSize(50),
                HorizontalOptions = LayoutOptions.Center
            };

            Button button = new Button
            {
                Text = "Click Me!",
                Font = Font.SystemFontOfSize(NamedSize.Large),
                BorderWidth = 1,
                HorizontalOptions = LayoutOptions.Center,
                VerticalOptions = LayoutOptions.CenterAndExpand
            };
            button.Clicked += OnButtonClicked;

            label = new Label
            {
                Text = "0 button clicks",
                Font = Font.SystemFontOfSize(NamedSize.Large),
                HorizontalOptions = LayoutOptions.Center,
                VerticalOptions = LayoutOptions.CenterAndExpand
            };

            // Accomodate iPhone status bar.
            this.Padding = new Thickness(10, Device.OnPlatform(20, 0, 0), 10, 5);

            // Build the page.
            this.Content = new StackLayout
            {
                Children = 
                {
                    header,
                    button,
                    label
                }
            };
        }

        void OnButtonClicked(object sender, EventArgs e)
        {
            clickTotal += 1;
            label.Text = String.Format("{0} button click{1}",
                                       clickTotal, clickTotal == 1 ? "" : "s");
        }
    }
}

Requirements

Namespace: Xamarin.Forms
Assembly: Xamarin.Forms.Core (in Xamarin.Forms.Core.dll)
Assembly Versions: 1.0.0.0, 1.1.0.0, 1.2.0.0, 1.3.0.0, 1.4.0.0, 1.5.0.0, 2.0.0.0

The members of Xamarin.Forms.Button are listed below.

See Also: View

Public Constructors

Initializes a new instance of the Button class.

Public Fields

static readonly
BorderColorPropertyBindableProperty. Backing store for the BorderColor bindable property.
static readonly
BorderRadiusPropertyBindableProperty. Backing store for the BorderRadius bindable property.
static readonly
BorderWidthPropertyBindableProperty. Backing store for the BorderWidth bindable property.
static readonly
CommandParameterPropertyBindableProperty. Backing store for the CommandParameter bindable property.
static readonly
CommandPropertyBindableProperty. Backing store for the Command bindable property.
static readonly
ContentLayoutPropertyBindableProperty. Backing store for the ContentLayout property.
static readonly
FontAttributesPropertyBindableProperty. Backing store for the FontAttributes property.
static readonly
FontFamilyPropertyBindableProperty. Backing store for the FontFamily property.
static readonly
FontPropertyBindableProperty. Backing store for the Font bindable property.
static readonly
FontSizePropertyBindableProperty. Backing store for the FontSize property.
static readonly
ImagePropertyBindableProperty. Backing store for the Image bindable property.
static readonly
TextColorPropertyBindableProperty. Backing store for the TextColor bindable property.
static readonly
TextPropertyBindableProperty. Backing store for the Text bindable property.

Public Properties

BorderColorColor. Gets or sets a color that describes the border stroke color of the button. This is a bindable property.
BorderRadiusInt32. Gets or sets the corner radius of the border. This is a bindable property.
BorderWidthDouble. Gets or sets the width of the border. This is a bindable property.
CommandICommand. Gets or sets the command to invoke when the button is activated. This is a bindable property.
CommandParameterObject. Gets or sets the parameter to pass to the Command property. This is a bindable property.
ContentLayoutButton+ButtonContentLayout. Gets or sets an object that controls the position of the button image and the spacing between the button's image and the button's text.
FontFont. Gets or sets the Font for the Label text. This is a bindable property.
FontAttributesFontAttributes. Gets a value that indicates whether the font for the button text is bold, italic, or neither.
FontFamilyString. Gets the font family to which the font for the button text belongs.
FontSizeDouble. Gets or sets the size of the font of the button text.
ImageFileImageSource. Gets or sets the optional image source to display next to the text in the Button. This is a bindable property.
TextString. Gets or sets the Text displayed as the content of the button. This is a bindable property.
TextColorColor. Gets or sets the Color for the text of the button. This is a bindable property.

Public Methods

On<T>() : IPlatformElementConfiguration<T, Button>
Returns the platform-specific instance of this Button, on which a platform-specific method may be called.

Protected Methods

override
OnBindingContextChanged()
Method that is raised when the binding context changes.
override
OnPropertyChanging(String)
Call this method from a child class to notify that a change happened on a property.

Public Events

ClickedOccurs when the Button is clicked.

Explicitly Implemented Interface Members