How to await asynchronous tasks in the constructor in C#

For original inspiration see this excellent link by Stephen Cleary: http://blog.stephencleary.com/2013/01/async-oop-2-constructors.html Step 1: Create a new WPF application Step 2: Define your example service In this example, a service to count the number of bytes in a web page: MyStaticService.cs Step 3: Define a type for obtaining the results or errors This method takes a …

Continue reading ‘How to await asynchronous tasks in the constructor in C#’ »

Using HttpClient to download files from the internet

A simple console application to demonstrate the downloading of files form the internet. It is pretty much the same as the example at the dot net pearls website, with some screenshots for further clarification. Step 1: Create a new console application: Step 2: Add the System.Net.Http reference Make sure this reference has been added to …

Continue reading ‘Using HttpClient to download files from the internet’ »

Running a WPF Progress bar as a background worker thread

How to run a WPF progress bar as a background worker thread. This code uses ICommand to respond to the user’s request to both and pause start the progress bar background process. Step 1: Create a new WPF application Step 2: Create the MainWindow.xaml user interface components Step 3: Create the ViewModel classes BaseViewModel.cs MainWindowViewModel.cs …

Continue reading ‘Running a WPF Progress bar as a background worker thread’ »

Binding the visibility of WPF elements to a property

How to set the visibility of your WPF user interface item so that it may be hidden/collapsed or shown as desired Step 1: Create a new WPF Application Step 2: Modify the MainWindow.xaml to create a User control Let’s keep it simple – a button: Step 3: Create the ViewModel classes This is primarily to …

Continue reading ‘Binding the visibility of WPF elements to a property’ »

Inserting button controls into a WPF ListView and handling their click events

Some instructions on how to : 1. Create a WPF ListView control containing a button in each row 2. Handle the event when the button in the ListView is clicked Step 1: Create the Visual Studio WPF project Step 2: Add event-handling and event-raising infrastructure Just add the following classes to your project: EventArgs.cs EventRaiser.cs …

Continue reading ‘Inserting button controls into a WPF ListView and handling their click events’ »

Displaying a WPF application icon in the notification area

A short set of instructions on how to get your WPF application to display its application icon in the notification area of your Windows screen (the area in the bottom right of your screen). Step 1: create a new WPF application Step 2: Ensure the necessary references are added In particular: Also make sure System.Drawing …

Continue reading ‘Displaying a WPF application icon in the notification area’ »

Setting the application icon in WPF XAML

Some instructions on how to set the application icon in a WPF / XAML application to something other than the default icon. Step 1: Create a new WPF application: Step 2: Right click your project folder and set properties: Select the Application tab: We are interested in configuring the ‘Icon and manifest’ section. Click the …

Continue reading ‘Setting the application icon in WPF XAML’ »

Recursively searching directories to find text within files in C#

A straightforward utility to recursively search for text contained within files and directories. I created a C# console application. The actual recursive routine to search the directories and subdirectories recursively is quite straightforward: Each directory is recursively searches as well as all the files contained within it. Full code listing So to search for all …

Continue reading ‘Recursively searching directories to find text within files in C#’ »