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

Logging information while debugging


Sometimes, it is inconvenient to place many breakpoints in the code to see how the application, or its particular module, is being executed. In such a case, it is a good idea to log various information and present it to the developer in the IDE in real time.

In this recipe, you will learn how to perform such a task using the Debug class from the System.Diagnostics namespace as well as present logs in the IDE within the Output window.

Getting ready

To complete this recipe, you need the project with two pages, represented by the MainPage and ProductsPage classes. It is necessary to pass a category identifier while navigating from MainPage to ProductsPage.

How to do it...

To prepare an example that shows how to log some data while debugging, you need to perform the following steps:

  1. Open the ProductsPage.xaml.cs file.

  2. To log information after clicking on the button, but before coming back to the dashboard, modify the code of the Button_Click method, as follows:

            private void Button_Click(object sender, RoutedEventArgs e) 
            { 
                Debug.WriteLine("Coming back to the dashboard"); 
                Frame.GoBack(); 
            } 
    

    Tip

    Do not forget to add the using statement for the System.Diagnostics namespace, using the following line of code:

    using System.Diagnostics;

    The IDE is equipped with a really nice feature that allows you to automatically insert a missing using statement. To do so, just left-click on the name of the unrecognized type and press Ctrl. (dot). In the pop-up window, you can find an option to automatically add a proper using statement.

  3. To add conditional logging, activated only if the associated Boolean expression is evaluated to true, modify the code of the OnNavigatedTo method in the ProductsPage.xaml.cs file, as follows:

            protected override void OnNavigatedTo( 
                NavigationEventArgs e) 
            { (...) 
                Debug.WriteLine($"Presenting a list of products  
                    from category #{categoryId}"); 
                Debug.WriteLineIf(categoryId < 0, $"Incorrect category  
                    identifier, namely {categoryId}"); 
            } 
    

  4. Start debugging the application.

  5. Open the Output window by navigating to Debug | Windows | Output from the menu. Then, click on the button in the emulator to see how the logs appear in the Output window. The exemplary result is shown as follows:

        Presenting a list of products from category #1 
        Coming back to the dashboard. 

See also

  • The Breakpoints-based debugging recipe

  • The Step-by-step debugging recipe

  • The Executing code while debugging recipe

  • The Creating a unit test and Running a set of tests recipes in Chapter 9, Testing and Submission