Properties and events:
Every control can be customized in two ways: by setting properties and actions. Both are identified by attributes of the XAML tag, but they have two different purposes.
Properties are used to change the look or the behavior of the control. Usually, a property is simply set by assigning a value to the specific attribute.
For example, if we want to assign a value to the Text property of a TextBlock control, we can do it in the following way:
<TextBlock Text="This is a text block" />
There’s also an extended syntax that can be used in the case of a complex property that can’t be defined with a plain string. For example, if we need to set an image as a control’s background, we need to use the following code:
<Grid>
<Grid.Background>
<ImageBrush ImageSource="/Assets/Background.png" />
</Grid.Background>
</Grid>
Complex properties are set by using a nested tag with the name of the control plus the name of the property, separated by a dot (to set the Background property of the Grid control, we use the Grid.Background syntax).
One important property that is shared by every control is x:Name, which is a string that univocally identifies the control in the page. You can’t have two controls with the same name in a single page. Setting this property is very important if you need to interact with the control in the code behind—you’ll be able to refer to it by using its name.
Events
Events are a way to manage user interactions with the control. One of the most used is Tap, which is triggered when users tap the control.
<Button Tap="OnButtonClicked" />
When you define an action, Visual Studio will automatically prompt you to create an event handler, which is the method (declared in the code behind) that is executed when the event is triggered.
private void OnButtonClicked(object sender, GestureEventArgs e)
{
MessageBox.Show("Hello world");
}
In the previous example, we display the classic “Hello world” message to users when the button is pressed.