C# Generic Method Resolution Gotcha

Tuesday, August 19, 2014

Yesterday, I broke some unit tests after removing an unused type parameter from a method, which seemed like a small and harmless code modification. Well, it turns out that it isn’t. If you want to know why, keep reading. Consider the following code snippet: 1: 2: public class Tests 3: { 4: 5: public void Test() 6: { 7: Assert.AreEqual("Int", DoSomething(1));...
tags: , ,
no comments

Setting a WPF Window to be Always on Top

Saturday, January 5, 2013

Some applications require that their main window will always be on top of all the other windows. Doing so in WPF is quite simple. In this post I will show how. In order for the window to be opened on top of all the other windows, set its Topmost property to True. Doing just that is not enough, because when the window will lose its focus, it will be placed behind the focused window. To keep the window top-most even after it loses its focus, add an EventTrigger to the window's LostFocus event, and set the Topmost property...
tags: , , ,
no comments

Automatic Properties are not Fields

Friday, December 28, 2012

Even though that the statement in the title is trivial, the fact that the syntax of automatic properties and fields is almost identical can sometimes make us forget that they are two different things. Take, for example, the following struct: 1: 2: public struct MyData 3: { 4: public int A; 5: public int...
tags: , ,
no comments

Compiler Error: Invalid Character in the Given Encoding

Friday, December 14, 2012

Recently, a colleague of mine received the above compilation error, together with hundreds of other, less helpful errors, when he tried to compile what seemed to be a perfectly fine XAML code. The problems started after he removed all the comments from the source files with a program that he created. The program simply loads the source file's content, removes the comments, and then writes the updated content back to the file. Here is a naive example to illustrate the problem: 1: public void ProcessFile(string path) ...
tags: , ,
no comments

InvalidOperationException Is Thrown by PlayToReceiver’s StartAsync Method

Tuesday, November 27, 2012

While I was playing around with the new Windows Store Play-To contract, I came across a strange exception when I used the PlayToReceiver class to register my app as a Play-To target. Here is the relevant code: 1: protected async override void OnNavigatedTo(NavigationEventArgs e) 2: { 3: if (m_playToReceiver != null) 4: return; ...
tags: , ,
no comments

WPF Command Confirmation

Thursday, October 25, 2012

In the last post, I introduced you to my DynamicGridFormationBehavior, which gives you the ability to dynamically change a grid's formation, using only Xaml. In this post, I want to introduce you to another behavior, called ConfirmationBehavior. First let's set the context. Assume that we have an application that handles customers' information, and allows CRUD operations on it. The client asked that before any critical operation on the data (deleting a customer for example), a confirmation message will appear. If you're following the MVVM pattern in your WPF applications (and you should), you will probably end up...
tags: , , ,
no comments

Changing WPF Grid Structure Dynamically

Friday, October 19, 2012

In an application I was working on recently, I needed to implement a split screen, like those on the closed-circuit cameras monitors. Usually, a simple Grid or a UniformGrid would do the trick, but in this case, the requirement was to allow the user to dynamically change the grid's structure. The following screenshots shows the grid's structure when the user chooses to display two, three, or four screens, respectively: As you can see, different user selections requires entirely different grid structure, with different number of...
tags: , , ,
no comments