Book Image

Windows Presentation Foundation Development Cookbook

Book Image

Windows Presentation Foundation Development Cookbook

Overview of this book

Windows Presentation Foundation (WPF) is Microsoft's development tool for building rich Windows client user experiences that incorporate UIs, media, and documents. With the updates in .NET 4.7, Visual Studio 2017, C# 7, and .NET Standard 2.0, WPF has taken giant strides and is now easier than ever for developers to use. If you want to get an in-depth view of WPF mechanics and capabilities, then this book is for you. The book begins by teaching you about the fundamentals of WPF and then quickly shows you the standard controls and the layout options. It teaches you about data bindings and how to utilize resources and the MVVM pattern to maintain a clean and reusable structure in your code. After this, you will explore the animation capabilities of WPF and see how they integrate with other mechanisms. Towards the end of the book, you will learn about WCF services and explore WPF's support for debugging and asynchronous operations. By the end of the book, you will have a deep understanding of WPF and will know how to build resilient applications.
Table of Contents (13 chapters)
2
Using WPF Standard Controls

There's more...

Each WPF application project created by Visual Studio using the default template consists of the following files:

  • App.config: This is the configuration file of your WPF application. By default, it contains the following lines that describe the supported runtime version for the application to run. This contains exactly the same runtime version that we selected during the project creation:
    <?xml version="1.0" encoding="utf-8" ?> 
      <configuration> 
        <startup>  
          <supportedRuntime            
version="v4.0"sku=".NETFramework,Version=v4.7" /> </startup> </configuration>

The config file can also contain application settings and other configuration settings that you want to use/refer in your application.

  • App.xaml: Visual Studio automatically creates the App.xaml file when you create a WPF project. It is the declarative starting point of your application. The root element of this file is the Application instance, which defines application specific properties and events:
    <Application x:Class="CH01.HelloWPFDemo.App" 
               
xmlns="http://schemas.microsoft.com/winfx
/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:CH01.HelloWPFDemo" StartupUri="MainWindow.xaml">
<Application.Resources> </Application.Resources> </Application>

The instance of the Application class defines the Window or a Page that's going to be the startup UI, and is registered with the StartupUri property. In the preceding code, (StartupUri="MainWindow.xaml") states that the MainWindow.xaml page will get loaded, once you run the application.

The application instance can also hold global/application-level resources (such as, Style, Template, and Converter) that can be used globally throughout the application.

  • App.xaml.cs: This is the code-behind class file of the App.xaml and extends the Application class of the framework to write application-specific code. You can use this file to subscribe to the events such as Startup, UnhandledException to perform common operations:
namespace CH01.HelloWPFDemo 
{ 
    /// <summary> 
    /// Interaction logic for App.xaml 
    /// </summary> 
    public partial class App : Application 
    { 
    } 
} 

This class is often used to manipulate command-line parameters and load different XAML pages based on that.

  • MainWindow.xaml: This is the default UI page that Visual Studio generates on creation of the WPF project. It is the page that gets registered as the StartupUri in App.xaml. The root element of this page is Window and it contains a Grid layout by default. Here is the default code snippet:
<Window x:Class="CH01.HelloWPFDemo.MainWindow" 
  xmlns=
"http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Grid> </Grid> </Window>

The x:Class attribute defines the associated partial class where the UI logic is being written. You can modify this XAML to provide a fresh look to your application start page. Various UI controls and layouts are going to be covered in the later chapters of this book.

  • MainWindow.xaml.cs: This is the code-behind class of MainWindow.xaml and contains the logic related to UI operations. In general, developers write implementations of various UI operations in this class.

Whenever you add any UI elements to an XAML page, the control gets registered internally in a partial class file that has .g.i.cs as the extension. For example, if you add a control in the MainWindow.xaml file, it gets registered in the MainWindow.g.i.cs residing in the obj folder. If you open the file, you can observe the entire loading process inside the InitializeComponent() method.