Raise PropertyChanged events without using strings in Prism

While I was testing something with Prism4 + MEF, I found this interesting method overload to raise PropertyChanged events without having to use the string name of the property itself.
Because having a typo in such a string can be very painful. :)

Let’s say you have a ViewModel which derives from the NotificationObject. In the setter I’m using the ‘better’ way of raising that PropertyChanged event:

public class TestViewModel : NotificationObject
{
private string _Title;

public string Title
{
get { return _Title; }
set
{
if (value != _Title;)
{
_Title = value;
this.RaisePropertyChanged(() => this.Name);
}
}

}
}

instead of writing:

this.RaisePropertyChanged("Title");

If you have already your project full of RaisePropertyChanged methods with strings you can easily replace them in Visual Studio with Regular Expressions:

- [Ctrl]+[F]
– Select [Quick Replace]
– In the dropdown menu choose [Replace in Files] instead of [Quick Replace]
– Look in: [Entire Solution]
– check [Use] and select [Regular expressions]
– Find what: RaisePropertyChanged\(\”{[:a]*}\”\)\;
– Replace with: RaisePropertyChanged(() => this.\1);

Have fun ;)

C# Expression Trees

Just a quick example that you can try yourself. It can be hard to write and debug Expression Trees but they are very powerful.

using System;
using System.Linq.Expressions;

namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
Func<int, int> f = (x) => x + 1; // Same as: x++
var result = f(1);
Console.WriteLine("My result: " + result);

// ------

var result2 = f.Invoke(1);
Console.WriteLine("My result (via Invoke): " + result2);

// ------

ParameterExpression param0 = Expression.Parameter(typeof(int)); // first parameter
ParameterExpression param1 = Expression.Parameter(typeof(int)); // second parameter
ConstantExpression const0 = Expression.Constant(1);             // a constant

BinaryExpression expr = Expression.Add(Expression.Subtract(param1, const0), param0); // Same as: param1 - constant + param0
Expression<Func<int, int, int>> lambdaExpr = Expression.Lambda<Func<int, int, int>>(expr, param0, param1); // creates the lambda delegate

Func<int, int, int> compiledExpr = lambdaExpr.Compile(); // compiles the expression tree
var result3 = compiledExpr(1, 1); // executes the epxression tree with param0 and param1

Console.WriteLine("My result via Expression: " + result3);

// Don't close the console directly
Console.ReadLine();
}
}
}

See also the ExpandoObject in C#.

Enjoy ;)

Select the index of a specific object via LINQ

This will search for the index of an object in a collection depending on a specific criteria.
When the object is not found in the collection, it will return an index with value -1.


string searchValue = "d";
List<string> items = new List<string>() { "a", "b", "c", "d", "e" };

int foundIndex = items.Select((x, i) => new { x, i })
.Where(x => x.x == searchValue)
.Select(x => x.i)
.DefaultIfEmpty(-1)
.First();

// specific code for LINQPad
foundIndex.Dump();

The result will be 3, because “d” has index 3 in the collection.

HTML5 support for Visual Studio 2010 Editor

Today, a faction of the Web Platform and Tools team, spearheaded by Mads Kristensen, is pleased to announce the Visual Studio Web Standards Update. This adds better support for HTML5, CSS3 and new JavaScript features to ALL versions of Visual Studio.

More info and the download link here.

[ source ]

Debugger Canvas for Visual Studio

Microsoft and Brown University have collaborated to create Debugger Canvas, a free Power Tool that adds Code Bubbles™ to Visual Studio for a new way to debug.

Do you get lost in the document tabs? Are you tired of the debugger jumping around from file to file? Debugger Canvas pulls together the code you’re exploring onto a single pan-and-zoom display. As you hit breakpoints or step into code, Debugger Canvas shows just the methods that you’re debugging, with call lines and local variables, to help you see the bigger picture.

Release info:

We are currently in the last phase of shipping! The first alpha release will be available in early June 2011. (download link)

Something I will check out for sure! ;)
Update: works only with Visual Studio 2010 Ultimate :(

More info on the Microsoft Research page.

[ source ]

White screen of Darn

Ever had a White screen of Darn (WSOD) in Visual Studio or an error like this: “exception of type system.componentmodel.design.exceptioncollection was thrown” ?

If the [Clean Solution] and [Rebuild Solution] didn’t work.
Here is how to find the culprit:

  1. Open Visual Studio 2010 and open/load your solution containing the problem
  2. Open a second Visual Studio 2010 instance

In the second VS2010 instance do the following things:

  1. Go to [Tools] > [Attach to Process...]
  2. Select devenv.exe with which contains the solution with the problem from the Available Processes and click OK
  3. Next go to [Debug] > [Exceptions...]
  4. Check [Common Language Runtime Exceptions] in the [Thrown] column

Now go to your first Visual Studio 2010 instance with your project and open the designer file which causes you the PITA (Pain In The Ass).

Your second VS2010 instance should break on the exception, showing you where it goes wrong in your code.

Enjoy ;)

Visual Studio Styles

I’ve been using a new style in Visual Studio recently, because my eyes got a bit too tired of looking constantly at the white background of the editor in Visual Studio 2010.

Today I’ve installed the style also at work and I must say that my eyes like it ^^.

I used “Son of Obsidian” from StudioStyl.es.

Installing this style is simple, if you have problems read the StudioStyl.es FAQ.

Happy coding ;)

[ source ]