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

How to do it...

Follow these steps to use a background worker, to perform the long running process, and to count the odd and even numbers within a range:

  1. From Solution Explorer, navigate to the MainWindow.xaml.cs file.
  2. Add the following using namespace—System.ComponentModel, so that we can use the BackgroundWorker class.
  3. Inside the OnCalculateClicked event, instead of calling ThreadPool to execute the operation, create an instance of the BackgroundWorker class.
  4. Register the worker events DoWork and RunWorkerCompleted.
  5. Call the RunWorkerAsync method of the background worker by passing the numeric range as an argument. The argument accepts objects, hence, we will use Tuple<int, int> as the data type for simplicity. The complete code looks as follows:
private void OnCalculateClicked(object sender,  
  RoutedEventArgs e) 
{ 
    totalOdd = 0; 
    totalEven = 0; 
 
    if (int.TryParse(fromValue.Text, out int from) && 
        int.TryParse(toValue.Text, out int to)...