Archive for August, 2010

Opening files closes VS2008

Once in a while I happen to have a project that simply closes when a file such as .resx or .xaml is openened in Visual Studio 2008 – even when there is nothing wrong with the files themselves.

When that happens, errors are logged to the Event Viewer, but they are not that helpful – for a change.

To avoid that error, there is a couple of ways. First of all, delete the .suo file in your solution folder. Deleting that file will do no harm to your solutions, as it only stores user-specific information, such as what are your opened files, which directories in Solution Explorer are collapsed or not, etc.

That usually works. However, not always, specially with .xaml files. If VS2008 insists in closing after deleting .suo, next time you open the .xaml file, simply press ESC repetitively, the way maniacs do. That will avoid the design view to be loaded – which apparently breaks VS2008. If you are successful in not seeing VS2008 to close again, you can close your file and re-open it. Hopefully.

What if Visual Studio still closes? Good luck, my friend.

Tags: , ,

String.Format() on XAML for WPF

Just found out some nice tricks on WPF, so here are some quick hints about using String.Format() with multiple parameters on XAML for WPF (and possibly Silverlight).

Say that you have a Phone class that contains three properties: CountryCode, AreaCode and Number. The DataContext for your Window contains a property of type Phone, and you want to print that object information in a pre-formatted way defined at the XAML file. The desired format is something like +1 55 55555555 (plus sign, followed by CountryCode, followed by a space, followed by AreaCode, followed by a space, followed by Number).

So instead of creating a property to hold the pre-formatted string at your class side, you just want to define it at XAML level.

Take the code below as an example:

<TextBlock>
	<TextBlock.Text>
		<MultiBinding StringFormat="+{0} {1} {2}">
			<Binding Path="Phone.CountryCode" />
			<Binding Path="Phone.AreaCode" />
			<Binding Path="Phone.Number" />
		</MultiBinding>
	</TextBlock.Text>
</TextBlock>

That will just do the trick for you!

However, we have some considerations to pay attention at. First of all:

<MultiBinding StringFormat="+{0} {1} {2}">

That line just works because of that trailing plus sign. If you were to use no plus sign (or any other prefixing string) at all and just display CountryCode directly, then you need a small tweak:

<MultiBinding StringFormat="{}{0} {1} {2}">

That’s the {} just before {0} parameter.

Furthermore, there are some nice attributes to add to the Binding element. For instance, if the value of your DataContext Phone object can’t be retrieved in design-time, you might see {DependencyProperty.UnsetValue} displayed on your design screen. TargetNullValue and FallbackValue can be added to fix that.

<TextBlock>
	<TextBlock.Text>
		<MultiBinding StringFormat="+{0} {1} {2}">
			<Binding Path="Phone.CountryCode" TargetNullValue="--" FallbackValue="??" />
			<Binding Path="Phone.AreaCode" TargetNullValue="--" FallbackValue="??" />
			<Binding Path="Phone.Number" TargetNullValue="--------" FallbackValue="????????" />
		</MultiBinding>
	</TextBlock.Text>
</TextBlock>

The first one is the substitution for null values, while the value of the second one is returned when no value can be found (such as when the bound property is not initialized in design-time). Just take care as that works on runtime as well!

Just some tricks particularly interesting during MVVM development. I hope it can be of some help!

Tags: , , , ,