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

Placing a control


The automatically generated page does not contain any content that could be interesting to a user. For this reason, it is important to learn how to place a new control.

By default, you have access to many controls that are available out of the box, such as a button, textbox, checkbox, list view, progress bar, calendar, or web view. Of course, these are only examples of the available controls. You can browse the full list in the Toolbox window within the IDE.

In this recipe, you will learn how to add the first button to the page.

Getting ready

To step through this recipe, you only need the automatically generated project.

How to do it...

To place a control on the page, you need to perform the following steps:

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

  2. Open the Toolbox window by navigating to View | Toolbox from the menu.

  3. Drag and drop the Button item from the Toolbox window to the graphical designer in the MainPage.xaml file. The control is now added, and you can easily adjust its location and size, as shown in the following image:

How it works...

Adding controls by dragging them from the Toolbox window causes the creation of a suitable part of the XAML code. In the case of the exemplary button, the following code is generated automatically:

    <Page 
     x:Class="CH01.MainPage" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:local="using:CH01" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:mc="http://schemas.openxmlformats.org/markup- 
               compatibility/2006" 
     mc:Ignorable="d"> 
        <Grid Background="{ThemeResource  
              ApplicationPageBackgroundThemeBrush}"> 
            <Button  
             x:Name="button" 
             Content="Button" 
             HorizontalAlignment="Left" 
             Margin="164,242,0,0" 
             VerticalAlignment="Top" /> 
        </Grid> 
    </Page> 

As you can see, the Button control is added within Grid. Its name is set to button (x:Name) and its content is set to the Button text (Content). Its location within the grid is specified by margins (Margin), given in the following order: left (164 pixels), top (242 pixels), right (0 pixels), and bottom (0 pixels). What is more, horizontal and vertical alignments are set (HorizontalAlignment and VerticalAlignment, respectively).

Note

Try to adjust the values of Margin, HorizontalAlignment, and VerticalAlignment on your own to get to know the impact of such properties on the button layout. You will learn more about various ways to place controls in the next chapter.

There's more...

The graphical editor available in Microsoft Visual Studio Community 2015 has a set of really useful features, such as the presentation of a UI for various screen resolutions. It is beneficial to learn more about such features in order to design a UI in a more efficient way.

See also

  • The Handling events recipe

  • The Adding a button, Adding a text block, Adding a textbox, Adding a password box, Adding a checkbox, Adding a combobox, Adding a listbox, Adding an image, Adding controls programmatically, and Creating and using a user control recipes in Chapter 2, Designing a User Interface