Blog: C#

Recommendations and best practices for implementing MVVM and XAML/.NET applications

January 30, 2015, (updated on April 11, 2017), 32 comments, Software Development

In this article I’ll describe the rules and practices I’m following for XAML and MVVM application development. I’m using the described techniques since multiple years and they have proven themselves for me. Most of the described rules apply for all types of XAML projects – Windows Store, Windows Phone, Silverlight and WPF projects. Sample code: The source code of the application Visual JSON Editor contains sample implementations for most of the discussed recommendations. Some sample code snippets in this article use the MVVM classes from the MyToolkit library which is developed by myself. Project file structure All MVVM/XAML applications should […] Read more...

Tags: , , , ,

How to mimic singlecast events in C#

January 12, 2015, (updated on February 13, 2015), Software Development

C# only supports multicast events and delegates. Even if you define a delegate property without the event keyword, it still remains multicasted: public class Foo { public Action MyEvent { get; set; } } var foo = new Foo(); foo.MyEvent += delegate { }; foo.MyEvent += delegate { }; The only difference between a regular property with a delegate type and an event property are: The event property can only be invoked by its class, the delegate in the regular property can also be invoked from outside (which may not be desired) Accessing and assigning an event property is only […] Read more...

Tags: , , ,

Implementation of “hybrid” (console or GUI) .NET applicatios

December 29, 2014, (updated on October 19, 2015), 1 comment, Software Development

Have you ever had the need to use an application (.exe) as a command line tool when started in the console (cmd.exe) and as GUI application when started from the Explorer shell? This article explains a possible implementation for this requirement. To be clear: I didn’t find the perfect solution; a Windows .NET application is either a console application or a Windows application. Either type can be used in the opposite mode, but both variants have some drawbacks. This article describes the solution which I think has the least disadvantages. The solution works by defining the application as console application […] Read more...

Tags: , , , ,

The correct handling of a WCF client’s life cycle

December 21, 2014, (updated on February 25, 2015), Software Development

The most intuitive way of handling a generated WCF client’s life cycle is by putting it into a using block: using (var svc = new MyServiceClient()) { var result = await svc.SumAsync(1, 2); } Note: You should not use static or singleton client objects. However, the problem with this code is, that IDisposable is not implemented correctly in the base class ClientBase. The problem is that the Dispose method always calls Close but Close may throw an exception. If this exception is thrown an additional call to Abort is needed but which is not made. Missing this Abort call may […] Read more...

Tags: , , ,

How to create an MSI installer using the WiX toolset

November 25, 2014, (updated on December 20, 2014), Software Development

This article provides a simple guide about how to quickly create a WiX installer project for an existing WPF application or another Windows desktop application. Continue reading at my company’s blog… Read more...

Tags: , , , , ,

Write detailed C# XML documentation and still keep the source code clean

October 20, 2014, (updated on January 12, 2015), Software Development

For my open-source project MyToolkit I write a lot of documentation. Currently the documentation for a class has to be updated in two locations: The source code and the project’s wiki. I asked myself how to avoid this and document everything in the source code file – while keeping the source code simple by not putting too much documentation in it. To write detailed documentation in your C# code, you can use the tags <remarks> and <example> (<summary> should always be there). To avoid polluting the source code file with documentation, you can additionally use the <include> tag to “outsource” […] Read more...

Tags: , , ,

Use JSON files with schema for application configuration

September 12, 2014, (updated on February 25, 2017), 2 comments, Software Development

Often, we face the problem that our customers need highly configurable applications but there is no time to develop rich configuration GUIs. This article describes a possible solution which is a compromise between editing raw configuration files and implementing rich configuration GUIs. The idea: First, implement some simple POCO classes (Plain Old CLR Object) representing the structure of the required configuration. Using a JSON serializer these classes can then be written and read from a JSON configuration file. Additionally, store an auto-generated JSON schema file alongside the configuration file. To edit the configuration file after its creation, use the tool […] Read more...

Tags: , , , ,

Develop an app which targets WP7 and WP8 (and WinRT) at the same time

August 16, 2013, (updated on September 8, 2014), Software Development

This post shows how to develop an app which targets the WP7 and WP8 platform at the same time. The idea is to create two projects – one for WP7 and one for WP8 – and link all files into one of the projects. This way you don’t have to maintain the code twice and only have to develop in one project. On Microsofts website you can find the base idea. However this article shows even more problems and their solutions when using this approach. Here are the steps to setup the solution: 1. Create the Visual Studio solution Create […] Read more...

Tags: , , ,

WinRT: Access string resources in a stongly typed way (C#)

January 21, 2013, (updated on January 30, 2015), Software Development

In WinRT/C# projects the .resx resource file is not available anymore. The new resource type is .resw which is directly supported by the XAML framework. The drawback of this new file format is that it does not automatically generate a code-behind C# class with a property for each key-value resource pair. The problem is that it is not possible to access these strings in a strongly typed manner. The only way to load resource strings is to load them by specifying the resource key as a string. The problem with this solution is that typing errors are not detected by […] Read more...

Tags: , ,

WinRT: Navigate to the previous page by pressing the back key

July 25, 2012, (updated on September 8, 2014), Software Development

If you are developing Metro applications, it is evident to not only have a good tablet experience but also a a good desktop (mouse and keyboard) experience. This is why I implemented a simple solution to navigate to the previous page if the user presses the back key. A lot of standard applications in Windows 8 support this feature already and I hope a lot of new applications will support it as well. The usage of my code is very simple: Just call the RegisterBackKey method if the page has been loaded – the rest will be handled by the […] Read more...

Tags: , , , ,