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...

Once the WPF project has been created, follow these steps to create a single instance of the WPF application:

  1. Run the application by pressing the CTRL + F5 key combination. This will launch one instance of the application.

  1. Press CTRL + F5 multiple times to launch multiple instances of the application. Now it's time to make the application a single instance application:
  2. Close all the running processes and then follow the next steps to implement the single instance behavior.
  3. Open the MainWindow.xaml and add the window title to Single Instance Demo. Here you can find the entire XAML code:
<Window x:Class="CH01.SingleInstanceDemo.MainWindow" 
  xmlns=
"http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Single Instance Demo" Height="250" Width="400"> <Grid> </Grid> </Window>
  1. Open the App.xaml.cs file and override the base implementation of the OnStartup method.
  2. Change the code of the OnStartup method so that it looks like the following code:
protected override void OnStartup(StartupEventArgs e) 
{ 
  base.OnStartup(e); 
 
  var mutex = new Mutex(true, "SingleInstanceDemo",  
  out bool isNewInstance); 
  if (!isNewInstance) 
  { 
    MessageBox.Show("Application instance is  
     already running!"); 
    Shutdown(); 
  } 
} 
  1. Add the System.Threading namespace declaration, so that the Mutex can be discoverable. The Mutex resides in the aforesaid namespace.
  2. Now compile the project to make sure that there are no compiler errors.
  3. Press CTRL + F5, which will run the first instance of the application.
  4. Now return to the Visual Studio, without closing the application, and then hit CTRL + F5. This time, instead of launching the application UI, an Application instance is already running! message will pop up on the screen. Clicking OK will close the message.
  5. Press CTRL + F5 again. Observe that no second instance of the UI is visible on the screen.