Button Class

Definition

A button View that reacts to touch events.

[Xamarin.Forms.RenderWith(typeof(Xamarin.Forms.Platform._ButtonRenderer))]
public class Button : Xamarin.Forms.View, Xamarin.Forms.IBorderElement, Xamarin.Forms.IButtonController, Xamarin.Forms.IElementConfiguration<Xamarin.Forms.Button>, Xamarin.Forms.IImageController, Xamarin.Forms.IImageElement, Xamarin.Forms.Internals.IFontElement
type Button = class
    inherit View
    interface IFontElement
    interface IBorderElement
    interface IButtonController
    interface IViewController
    interface IVisualElementController
    interface IElementController
    interface IElementConfiguration<Button>
    interface IImageController
    interface IImageElement
Inheritance
Attributes
Implements

Remarks

Button Appearance

In addition to the appearance properties common to all View objects, the Button class provides the BorderColor, BorderWidth, BorderRadius, and Image properties.

The XAML code snippet below demonstrates how to use these properties to create a basic button with the text "Press me!" The image that the button displays appears on the start side of the layout, which is the left side for left-to-right languages.


<Button Text="Press me!"
        Image="icon.png"
        BackgroundColor="White"
        BorderRadius="10"
        BorderWidth="2"
        BorderColor="Black"
        HorizontalOptions="Center"/>
To display the icon.png image with the above XAML, the developer must place it in the appropriate location, with the appropriate build action, specific to each platform project. The Images topic explains this and other important details about working with images in Xamarin.Forms.

Button Events

Buttons notify the developer of press, release, and click events with the Pressed, Released, and Clicked events, respectively.

The following XAML assigns handlers to two of these button events:


<Button x:Name="PressMeButton"
        Text="Press Me!"
        Pressed="PressMeButton_Pressed"
        Clicked="PressMeButton_Clicked"/>

With the XAML above, the following event handlers in the code-behind file change the button's text when these events are raised:

private void PressMeButton_Pressed(object sender, EventArgs e)
{
   (sender as Button).Text = "You pressed me!";
}
    
private void PressMeButton_Clicked(object sender, EventArgs e)
{
   (sender as Button).Text = "I was just clicked!";
}

Commands

The commanding API for buttons allows the developer to run a command in response to what would otherwise be the Clicked event.

The following XAML specifes a command to run when the button is clicked. The command updates a view model property called modelUpdateCount, which the button then uses to update its text. (The text binding is shown for completeness. Only the commanding code is relevant.):


<Button Text="{Binding modelUpdateCount, StringFormat='Number of updates to the model: {0:D}'}"
        Command="{Binding IncrementLabelCommand}" />

With the XAML above, the developer first defines the IncrementLabelCommand property on the view model and implements a method with which to later instantiate the command:

public ICommand IncrementLabelCommand { get; private set; }
    
void IncrementLabel()
{
   modelUpdateCount++;
}

Second, the developer initializes the command with the method (or action):

public ViewModelDemo()
{
   IncrementLabelCommand = new Command(IncrementLabel);
}

Finally, the developer must sets the binding context in the constructor for the main page to that of the view model where the property is implemented:

public MainPage()
{
    InitializeComponent();
    BindingContext = new ViewModelDemo();
}

Constructors

Button()

Initializes a new instance of the Button class.

Fields

BorderColorProperty

Backing store for the BorderColor bindable property.

BorderRadiusProperty

Backing store for the BorderRadius bindable property.

BorderWidthProperty

Backing store for the BorderWidth bindable property.

CommandParameterProperty

Backing store for the CommandParameter bindable property.

CommandProperty

Backing store for the Command bindable property.

ContentLayoutProperty

Backing store for the ContentLayout property.

CornerRadiusProperty

Backing store for the CornerRadius property.

FontAttributesProperty

Backing store for the FontAttributes property.

FontFamilyProperty

Backing store for the FontFamily property.

FontProperty

Backing store for the Font bindable property.

FontSizeProperty

Backing store for the FontSize property.

ImageProperty

Backing store for the Image bindable property.

ImageSourceProperty
IsPressedProperty

The backing store for the IsPressed field.

PaddingProperty

The backing store for the Padding property.

TextColorProperty

Backing store for the TextColor bindable property.

TextProperty

Backing store for the Text bindable property.

Properties

AnchorX

Gets or sets the X component of the center point for any transform, relative to the bounds of the element. This is a bindable property.

(Inherited from VisualElement)
AnchorY

Gets or sets the Y component of the center point for any transform, relative to the bounds of the element. This is a bindable property.

(Inherited from VisualElement)
AutomationId

Gets or sets a value that allows the automation framework to find and interact with this element.

(Inherited from Element)
BackgroundColor

Gets or sets the color which will fill the background of a VisualElement. This is a bindable property.

(Inherited from VisualElement)
Batched

For internal use by the Xamarin.Forms platform.

(Inherited from VisualElement)
Behaviors

Gets the list of Behavior associated to this element. This is a bindable propery.

(Inherited from VisualElement)
BindingContext

Gets or sets object that contains the properties that will be targeted by the bound properties that belong to this BindableObject.

(Inherited from BindableObject)
BorderColor

Gets or sets a color that describes the border stroke color of the button. This is a bindable property.

BorderRadius

Gets or sets the corner radius of the border. This is a bindable property.

BorderWidth

Gets or sets the width of the border. This is a bindable property.

Bounds

Gets the bounds of the element.

(Inherited from VisualElement)
class (Inherited from NavigableElement)
ClassId

Gets or sets a value used to identify a collection of semantically similar elements.

(Inherited from Element)
Command

Gets or sets the command to invoke when the button is activated. This is a bindable property.

CommandParameter

Gets or sets the parameter to pass to the Command property. This is a bindable property.

ContentLayout

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.

CornerRadius

Gets or sets the corner radius for the button, in device-independent units.

DisableLayout

For internal use by the Xamarin.Forms platform.

(Inherited from VisualElement)
EffectControlProvider

For internal use by the Xamarin.Forms platform.

(Inherited from Element)
Effects

A list of the effects that are applied to this item.

(Inherited from Element)
FlowDirection

Gets or sets the layout flow direction.

(Inherited from VisualElement)
Font

Gets or sets the Font for the Label text. This is a bindable property.

FontAttributes

Gets a value that indicates whether the font for the button text is bold, italic, or neither.

FontFamily

Gets the font family to which the font for the button text belongs.

FontSize

Gets or sets the size of the font of the button text.

GestureController

Gets the gesture controller for the view.

(Inherited from View)
GestureRecognizers

The collection of gesture recognizers associated with this view.

(Inherited from View)
Height

Gets the current rendered height of this element. This is a read-only bindable property.

(Inherited from VisualElement)
HeightRequest

Gets or sets the desired height override of this element.

(Inherited from VisualElement)
HorizontalOptions

Gets or sets the LayoutOptions that define how the element gets laid in a layout cycle. This is a bindable property.

(Inherited from View)
Id

Gets a value that can be used to uniquely identify an element through the run of an application.

(Inherited from Element)
Image

Gets or sets the optional image source to display next to the text in the Button. This is a bindable property.

ImageSource
InputTransparent

Gets or sets a value indicating whether this element should be involved in the user interaction cycle. This is a bindable property.

(Inherited from VisualElement)
IsEnabled

Gets or sets a value indicating whether this element is enabled in the user interface. This is a bindable property.

(Inherited from VisualElement)
IsFocused

Gets a value indicating whether this element is focused currently. This is a bindable property.

(Inherited from VisualElement)
IsInNativeLayout

For internal use by the Xamarin.Forms platform.

(Inherited from VisualElement)
IsNativeStateConsistent

For internal use by the Xamarin.Forms platform.

(Inherited from VisualElement)
IsPlatformEnabled

For internal use by the Xamarin.Forms platform.

(Inherited from VisualElement)
IsPressed
IsTabStop

Gets or sets a value that indicates whether this element is included in tab navigation. This is a bindable property.

(Inherited from VisualElement)
IsVisible

Gets or sets a value that determines whether this elements should be part of the visual tree or not. This is a bindable property.

(Inherited from VisualElement)
LogicalChildren

For internal use by the Xamarin.Forms platform.

(Inherited from Element)
Margin

Gets or sets the margin for the view.

(Inherited from View)
MinimumHeightRequest

Gets or sets a value which overrides the minimum height the element will request during layout.

(Inherited from VisualElement)
MinimumWidthRequest

Gets or sets a value which overrides the minimum width the element will request during layout.

(Inherited from VisualElement)
Navigation (Inherited from NavigableElement)
NavigationProxy (Inherited from NavigableElement)
Opacity

Gets or sets the opacity value applied to the element when it is rendered. This is a bindable property.

(Inherited from VisualElement)
Padding

Gets or sets the padding for the button.

Parent

Gets or sets the parent element of the element.

(Inherited from Element)
ParentView

Gets the element which is the closest ancestor of this element that is a VisualElement.

(Inherited from Element)
Platform (Inherited from Element)
RealParent

For internal use by the Xamarin.Forms platform.

(Inherited from Element)
Resources

Gets or sets the local resource dictionary.

(Inherited from VisualElement)
Rotation

Gets or sets the rotation about the Z-axis (affine rotation) when the element is rendered.

(Inherited from VisualElement)
RotationX

Gets or sets the rotation about the X-axis (perspective rotation) when the element is rendered.

(Inherited from VisualElement)
RotationY

Gets or sets the rotation about the Y-axis (perspective rotation) when the element is rendered.

(Inherited from VisualElement)
Scale

Gets or sets the scale factor applied to the element.

(Inherited from VisualElement)
ScaleX

Gets or sets a scale value to apply to the X direction.

(Inherited from VisualElement)
ScaleY

Gets or sets a scale value to apply to the Y direction.

(Inherited from VisualElement)
Style (Inherited from NavigableElement)
StyleClass (Inherited from NavigableElement)
StyleId

Gets or sets a user defined value to uniquely identify the element.

(Inherited from Element)
TabIndex (Inherited from VisualElement)
Text

Gets or sets the Text displayed as the content of the button. This is a bindable property.

TextColor

Gets or sets the Color for the text of the button. This is a bindable property.

TranslationX

Gets or sets the X translation delta of the element.

(Inherited from VisualElement)
TranslationY

Gets or sets the Y translation delta of the element.

(Inherited from VisualElement)
Triggers

Gets the list of Trigger associated to this element. This is a bindable property.

(Inherited from VisualElement)
VerticalOptions

Gets or sets the LayoutOptions that define how the element gets laid in a layout cycle. This is a bindable property.

(Inherited from View)
Visual (Inherited from VisualElement)
Width

Gets the current rendered width of this element. This is a read-only bindable property.

(Inherited from VisualElement)
WidthRequest

Gets or sets the desired width override of this element.

(Inherited from VisualElement)
X

Gets the current X position of this element. This is a read-only bindable property.

(Inherited from VisualElement)
Y

Gets the current Y position of this element. This is a read-only bindable property.

(Inherited from VisualElement)

Methods

ApplyBindings()

Apply the bindings to BindingContext.

(Inherited from BindableObject)
BatchBegin()

Signals the start of a batch of changes to the elements properties.

(Inherited from VisualElement)
BatchCommit()

Signals the end of a batch of commands to the element and that those commands should now be committed.

(Inherited from VisualElement)
ChangeVisualState()
ClearValue(BindableProperty)

Clears any value set by SetValue for property.

(Inherited from BindableObject)
ClearValue(BindablePropertyKey)

Clears any value set by SetValue for the property that is identified by propertyKey.

(Inherited from BindableObject)
Descendants()

For internal use by the Xamarin.Forms platform.

(Inherited from Element)
EffectIsAttached(String)

For internal use by the Xamarin.Forms platform.

(Inherited from Element)
FindByName(String)

Returns the element that has the specified name.

(Inherited from Element)
Focus()

Attemps to set focus to this element.

(Inherited from VisualElement)
GetChildElements(Point)

Returns the child elements that are visually beneath the specified point.

(Inherited from View)
GetSizeRequest(Double, Double)

Returns the SizeRequest of the element. Calling this method begins the measure pass of a layout cycle.

(Inherited from VisualElement)
GetValue(BindableProperty)

Returns the value that is contained in the BindableProperty.

(Inherited from BindableObject)
GetValues(BindableProperty, BindableProperty)

For internal use by the Xamarin.Forms platform.

(Inherited from BindableObject)
GetValues(BindableProperty, BindableProperty, BindableProperty)

For internal use by the Xamarin.Forms platform.

(Inherited from BindableObject)
InvalidateMeasure()

Method that is called to invalidate the layout of this VisualElement. Raises the MeasureInvalidated event.

(Inherited from VisualElement)
InvalidateMeasureNonVirtual(InvalidationTrigger)

For internal use by the Xamarin.Forms platform.

(Inherited from VisualElement)
IsSet(BindableProperty)

Returns true if the target property exists and has been set.

(Inherited from BindableObject)
Layout(Rectangle)

Updates the bounds of the element during the layout cycle.

(Inherited from VisualElement)
Measure(Double, Double, MeasureFlags)

Returns the minimum size that a visual element needs in order to be displayed on the device.

(Inherited from VisualElement)
NativeSizeChanged()

For internal use by the Xamarin.Forms platform.

(Inherited from VisualElement)
On<T>()

Returns the platform-specific instance of this Button, on which a platform-specific method may be called.

OnBindingContextChanged()

Method that is raised when the binding context changes.

OnChildAdded(Element)

Application developers can override this method to respond when a child is added.

(Inherited from VisualElement)
OnChildRemoved(Element)

Application developers can override this method to respond when a child is removed.

(Inherited from VisualElement)
OnChildrenReordered()

Invoked whenever the ChildrenReordered event is about to be emitted. Implement this method to add class handling for this event.

(Inherited from VisualElement)
OnMeasure(Double, Double)

Method that is called when a layout measurement happens.

(Inherited from VisualElement)
OnParentSet() (Inherited from NavigableElement)
OnPropertyChanged(String)

Method that is called when a bound property is changed.

(Inherited from Element)
OnPropertyChanging(String)

Call this method from a child class to notify that a change is going to happen on a property.

(Inherited from BindableObject)
OnSizeAllocated(Double, Double)

This method is called when the size of the element is set during a layout cycle. This method is called directly before the SizeChanged event is emitted. Implement this method to add class handling for this event.

(Inherited from VisualElement)
OnSizeRequest(Double, Double)

This method is called during the measure pass of a layout cycle to get the desired size of an element.

(Inherited from VisualElement)
OnTabIndexPropertyChanged(Int32, Int32) (Inherited from VisualElement)
OnTabStopPropertyChanged(Boolean, Boolean) (Inherited from VisualElement)
RemoveBinding(BindableProperty)

Removes a previously set binding.

(Inherited from BindableObject)
RemoveDynamicResource(BindableProperty)

Removes a previously set dynamic resource

(Inherited from Element)
SendClicked()

For internal use by the Xamarin.Forms platform.

SendPressed()

For internal use by the Xamarin.Forms platform.

SendReleased()

For internal use by the Xamarin.Forms platform.

SetBinding(BindableProperty, BindingBase)

Assigns a binding to a property.

(Inherited from BindableObject)
SetDynamicResource(BindableProperty, String)

Sets the BindableProperty property of this element to be updated via the DynamicResource with the provided key.

(Inherited from Element)
SetValue(BindableProperty, Object)

Sets the value of the specified property.

(Inherited from BindableObject)
SetValue(BindablePropertyKey, Object)

Sets the value of the propertyKey.

(Inherited from BindableObject)
SetValueCore(BindableProperty, Object, SetValueFlags)

For internal use by the Xamarin.Forms platform.

(Inherited from BindableObject)
SetValueFromRenderer(BindableProperty, Object)

For internal use by the Xamarin.Forms platform.

(Inherited from Element)
SetValueFromRenderer(BindablePropertyKey, Object)

For internal use by the Xamarin.Forms platform.

(Inherited from Element)
SizeAllocated(Double, Double)

SizeAllocated is called during a layout cycle to signal the start of a sub-tree layout.

(Inherited from VisualElement)
TabIndexDefaultValueCreator() (Inherited from VisualElement)
TabStopDefaultValueCreator() (Inherited from VisualElement)
UnapplyBindings()

Unapplies all previously set bindings.

(Inherited from BindableObject)
Unfocus()

Unsets focus to this element.

(Inherited from VisualElement)

Events

BatchCommitted

For internal use by the Xamarin.Forms platform.

(Inherited from VisualElement)
BindingContextChanged

Raised whenever the BindingContext property changes.

(Inherited from BindableObject)
ChildAdded

Occurs whenever a child element is added to the element.

(Inherited from Element)
ChildRemoved

Occurs whenever a child element is removed from the element.

(Inherited from Element)
ChildrenReordered

Occurs when the Children of a VisualElement have been re-ordered.

(Inherited from VisualElement)
Clicked

Occurs when the Button is clicked.

DescendantAdded

Occurs whenever a child element is added to the elements subtree.

(Inherited from Element)
DescendantRemoved

Occurs whenever a child element is removed from the elements subtree.

(Inherited from Element)
FocusChangeRequested

For internal use by the Xamarin.Forms platform.

(Inherited from VisualElement)
Focused

Occurs when the element receives focus.

(Inherited from VisualElement)
MeasureInvalidated

Event that is raised when the layout of a visual element is invalidated.

(Inherited from VisualElement)
PlatformSet (Inherited from Element)
Pressed

Occurs when the Button is pressed.

PropertyChanged

Raised when a property has changed.

(Inherited from BindableObject)
PropertyChanging

Raised when a property is about to change.

(Inherited from BindableObject)
Released

Occurs when the Button is released.

SizeChanged

Occurs when either the Width or the Height properties change value on this element.

(Inherited from VisualElement)
Unfocused

Occurs when the element loses focus.

(Inherited from VisualElement)

Explicit Interface Implementations

IBorderElement.BorderColorDefaultValue
IBorderElement.BorderWidthDefaultValue
IBorderElement.CornerRadiusDefaultValue
IBorderElement.IsBackgroundColorSet()
IBorderElement.IsBorderColorSet()
IBorderElement.IsBorderWidthSet()
IBorderElement.IsCornerRadiusSet()
IBorderElement.OnBorderColorPropertyChanged(Color, Color)
IDynamicResourceHandler.SetDynamicResource(BindableProperty, String)

For internal use by the Xamarin.Forms platform.

(Inherited from BindableObject)
IElementController.SetValueFromRenderer(BindableProperty, Object)

For internal use by the Xamarin.Forms platform.

(Inherited from Element)
IFontElement.FontSizeDefaultValueCreator()

For internal use by the Xamarin.Forms platform.

IFontElement.OnFontAttributesChanged(FontAttributes, FontAttributes)

For internal use by the Xamarin.Forms platform.

IFontElement.OnFontChanged(Font, Font)

For internal use by the Xamarin.Forms platform.

IFontElement.OnFontFamilyChanged(String, String)

For internal use by the Xamarin.Forms platform.

IFontElement.OnFontSizeChanged(Double, Double)

For internal use by the Xamarin.Forms platform.

IGestureController.CompositeGestureRecognizers

For internal use by the Xamarin.Forms platform.

(Inherited from View)
IImageController.SetIsLoading(Boolean)
IImageElement.Aspect
IImageElement.IsOpaque
IImageElement.OnImageSourcesSourceChanged(Object, EventArgs)
IImageElement.RaiseImageSourcePropertyChanged()
IImageElement.Source
INameScope.RegisterName(String, Object)

For internal use only.

(Inherited from Element)
IVisualElementController.EffectiveFlowDirection

Gets the effective visual flow direction for the element on the platform, taking into account the locale and logical flow settings.

(Inherited from VisualElement)
IVisualElementController.InvalidateMeasure(InvalidationTrigger)

This method is for internal use.

(Inherited from VisualElement)

Extension Methods

AbortAnimation(IAnimatable, String)

Stops the animation.

Animate(IAnimatable, String, Action<Double>, Double, Double, UInt32, UInt32, Easing, Action<Double,Boolean>, Func<Boolean>)

Sets the specified parameters and starts the animation.

Animate(IAnimatable, String, Action<Double>, UInt32, UInt32, Easing, Action<Double,Boolean>, Func<Boolean>)

Sets the specified parameters and starts the animation.

Animate(IAnimatable, String, Animation, UInt32, UInt32, Easing, Action<Double,Boolean>, Func<Boolean>)

Sets the specified parameters and starts the animation.

Animate<T>(IAnimatable, String, Func<Double,T>, Action<T>, UInt32, UInt32, Easing, Action<T,Boolean>, Func<Boolean>)

Sets the specified parameters and starts the animation.

AnimateKinetic(IAnimatable, String, Func<Double,Double,Boolean>, Double, Double, Action)

Sets the specified parameters and starts the kinetic animation.

AnimationIsRunning(IAnimatable, String)

Returns a Boolean value that indicates whether or not the animation that is specified by handle is running.

SetBinding(BindableObject, BindableProperty, String, BindingMode, IValueConverter, String)

Creates and applies a binding to a property.

SetBinding<TSource>(BindableObject, BindableProperty, Expression<Func<TSource,Object>>, BindingMode, IValueConverter, String)

Creates and applies a binding from an expression.

FindByName<T>(Element, String)

Returns the instance of type T that has name name in the scope that includes element.

FindNextElement(ITabStopElement, Boolean, IDictionary<Int32,List<ITabStopElement>>, Int32)
GetSortedTabIndexesOnParentPage(VisualElement, Int32)
GetTabIndexesOnParentPage(ITabStopElement, Int32, Boolean)
FadeTo(VisualElement, Double, UInt32, Easing)

Returns a task that performs the fade that is described by the opacity, length, and easing parameters.

LayoutTo(VisualElement, Rectangle, UInt32, Easing)

Returns a task that eases the bounds of the VisualElement that is specified by the view to the rectangle that is specified by the bounds parameter.

RelRotateTo(VisualElement, Double, UInt32, Easing)

Rotates the VisualElement that is specified by view from its current rotation by drotation.

RelScaleTo(VisualElement, Double, UInt32, Easing)

Returns a task that scales the VisualElement that is specified by view from its current scale to dscale.

RotateTo(VisualElement, Double, UInt32, Easing)

Returns a task that performs the rotation that is described by the rotation, length, and easing parameters.

RotateXTo(VisualElement, Double, UInt32, Easing)

Returns a task that skews the Y axis by opacity, taking time length and using easing.

RotateYTo(VisualElement, Double, UInt32, Easing)

Returns a task that skews the X axis by opacity, taking time length and using easing.

ScaleTo(VisualElement, Double, UInt32, Easing)

Returns a task that scales the VisualElement that is specified by view to the absolute scale factor scale.

TranslateTo(VisualElement, Double, Double, UInt32, Easing)

Animates an elements TranslationX and TranslationY properties from their current values to the new values. This ensures that the input layout is in the same position as the visual layout.

HasVisualStateGroups(VisualElement)

Returns true if element has one or more visual state groups associated with it. Otherwise, returns false.

Applies to