作者名:Braulio Diez Botella José Fernando Almoguera Pablo Nú?ez Sebastian Stehle Rocio Serrano Rudilla Reyes García Rosado
本章字數:242字
更新時間:2021-08-20 15:36:39
Interacting with Code-Behind
In the following example, we will allow the user to change the message Hello World! to one of their choice. Starting from the previous example, we will follow the given steps:
Open the mainpage.xaml file.
Replace the XAML code inserted in the previous example by the one highlighted as follows (we have added an additional textbox, a button, and identifiers for the controls).
When we build and execute the project, we realize that our window now has the aspect as shown in the following screenshot:
Next, we must implement the response to the Click event of the Change button.
Hook to the Click event directly in the XAML file. As soon as we start typing, IntelliSense (Microsoft's implementation of autocompletion) will ask us if we want to create the method (hitting Enter or Tab would create the method with the default name or with the name of the control after selecting a Click event).
Execute the same operation from the Properties panel (or by directly double-clicking on the button control):
As a result, XAML will look as follows:
<Button Content="Change" Click="Button_Click"/>
In Code-Behind, in the method invoked by the Click event, we must add a line of code, which transfers the text content entered by the user to the tag where we showed 'Hello World'.