Book Image

Windows Application Development Cookbook

By : Marcin Jamro
Book Image

Windows Application Development Cookbook

By: Marcin Jamro

Overview of this book

Need to ensure you can always create the best Windows apps regardless of platform? What you need are solutions to the biggest issues you can face, so you can always ensure you’re making the right choices and creating the best apps you can. The book starts with recipes that will help you set up the integrated development environment before you go ahead and design the user interface. You will learn how to use the MVVM design pattern together with data binding, as well as how to work with data in different file formats. Moving on, you will explore techniques to add animations and graphics to your application, and enable your solution to work with multimedia content. You will also see how to use sensors, such as an accelerometer and a compass, as well as obtain the current GPS location. You will make your application ready to work with Internet-based scenarios, such as composing e-mails or downloading files, before finally testing the project and submitting it to the Windows Store. By the end of the book, you will have a market-ready application compatible across different Windows devices, including smartphones, tablets, and desktops.
Table of Contents (17 chapters)
Windows Application Development Cookbook
Credits
About the Author
Acknowledgements
About the Reviewer
www.PacktPub.com
Preface

Handling events


Apart from adding controls to the page, it is necessary to introduce interaction with them, such as performing some actions after clicking on a button or choosing an item from a drop-down list. In this recipe, you will learn how to handle the event of pressing the button.

Getting ready

To use this recipe, you need the project from the previous recipe. It should contain the MainPage.xaml file with the added button.

How to do it...

To handle the Click event, you need to perform the following steps:

  1. Double-click on the MainPage.xaml file in the Solution Explorer window.

  2. To generate a method that handles the situation of the button being pressed, double-click on the added button. Alternatively, click on the button (either in a graphical designer or the XAML code) and then double-click on the Click field in the Properties window with the Event handlers for the selected element option (the lightning icon) selected, as shown in the following screenshot:

How it works...

Generating the method for handling the event of the button being clicked causes a modification, which is automatically introduced in both the MainPage.xaml and MainPage.xaml.cs files.

In the first file, with the XAML code describing the UI of the page, the Click property is automatically added to the Button element. It specifies the name of the method that is called when the user clicks on the button. The exemplary code is as follows:

    <Page (...)> 
        <Grid (...)> 
            <Button  
             x:Name="button" 
             Content="Button" 
             HorizontalAlignment="Left" 
             Margin="164,242,0,0" 
             VerticalAlignment="Top" 
             Click="button_Click" /> 
        </Grid> 
    </Page> 

It is worth mentioning that the button_Click method must exist in the MainPage class (name set as x:Class in the Page element). This method is also automatically generated in the MainPage.xaml.cs file, as follows:

    private void button_Click(object sender, RoutedEventArgs e) { } 

The method has a name that contains the name of the button (button) as well as information about the kind of event (Click). It has two parameters:

  • sender: This is an object that represents the clicked element, which you can cast to Button as (Button)sender

  • e: This represents the additional arguments regarding the operation

To specify operations that should be performed after pressing the button, you just need to add suitable C# code as the body of the button_Click method.

Tip

You could easily jump from the editor with the XAML code to the C#-based method definition by right-clicking its name defined in the .xaml file (within Click) and choosing Go To Definition from the context menu. Another solution is to click on such a name and press F12.

This way of handling the button being pressed is not the only possible one. Later in the book, you will learn how to use the data binding mechanism together with commands and the MVVM design pattern to improve the solution.

See also

  • The Placing a control recipe

  • The Creating the view model for a page and Introducing bindings and commands recipes in Chapter 3, MVVM and Data Binding