Tuesday, June 16, 2009

Why I'm having difficulties with WPF

Through Twitter I stumbled upon this article: http://www.thejoyofcode.com/If_you_cant_beat_XAML_improve_it.aspx

I think it’s an interesting post. Below you find my comments on that post. Any thoughts on the issue?

I am a very experienced programmer and have worked on a great number of languages (C, Basic, Smalltalk, Pascal, Fortran, Cobol, C++,, C#). Recently I started working on WPF. And I am one of those developers that is experiencing more difficulty grasping WPF than I did with other languages.

I don't think that it's because of XAML per se though. I don't mind typing in long XML element names and attributes. I don't mind writing all the end tags. I don't mind typing at all. Visual Studio / DevExpress' CodeRush offers me all the aide that I need for that. Code completion, templates, short cuts, et cetera make that real easy.

What I do have problems with is the actual concepts in WPF. One thing that I find really difficult to learn and to accept really is the fact that one must/can implement a lot of functionality in the XAML, which in my mental model represents the display part of the application, not the code. I seem to be that old fashion developer that wants to type a=b if I want to assign the value of b to a.

WPF/XAML drives me away from that mental model and I'm very much aware of that. It is beginning to win its place in my brains, but it's taking longer than I thought. Even being aware that another mental model is needed for WPF doesn't make it easier for me.

Maybe XAML *is* for designers. That is why I'm trying to work more closely with our visual designer (i.e. having him learn Blend) and leave more of the work to him.

But, it's really new and sometimes hard to go this way. I'm so much used to being able to do everything in our software. That is no longer the case. I will get used to it, but it may take me a bit more time.

My 2cts worth.

Bye,
Bart

Monday, June 15, 2009

How to crash Visual Studio…

Update. Thanks to my new friend Josh Twist (http://www.thejoyofcode.com) I found out that a hotfix by Microsoft solves this issue. Based on the articles on his website I managed to tidy up my XAML-code to. You can find information about the hotfix at http://support.microsoft.com/kb/958017/ and download it from http://code.msdn.microsoft.com/KB958017/Release/ProjectReleases.aspx?ReleaseId=1719.

In a previous post I stated that Visual Studio never crashed on me. I have to take that back since it now happens to me a lot.

I am pretty sure that it is because I’m doing all kind of things wrong in my code (I still need to learn WPF), but VS should never crash because I mistype some code. Below is a code snippet from a XAML-file that crashes VS every time. If I open up the XAML file in MS Blend, I get an error message (probably pointing into the right direction of the error):

TargetType van ControlTemplate BoardPiece komt niet overeen met sjabloontype TagVisualization.

(which is Dutch for telling me the TargetType of the ControlType doesn’t match the TemplateType TagVisualization).

Below is the contents of the XAML file. IsAcceptingNewRelation is a user defined dependency property. I specified the Oase.BoardPiece type in the TargetType of the ControlTemplate for the code to solve the IsAcceptingNewRelation dependency property of my TagVisualization. There probably is another/proper way of doing that, but I’m getting WPF blind. Anyway, this crashes VS if I take it to the design mode.

1: <s:TagVisualization
2: xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3: xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4: xmlns:s="http://schemas.microsoft.com/surface/2008"
5: xmlns:local="clr-namespace:Oase"
6: xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
7: xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
8: x:Class="Oase.BoardPiece"
9: mc:Ignorable="d"
10: s:Contacts.ContactDown="TagVisualization_ContactDown"
11: s:Contacts.ContactChanged="TagVisualization_ContactChanged"
12: s:Contacts.ContactUp="TagVisualization_ContactUp"
13: >
14:
15: <s:TagVisualization.Resources>
16: <ImageBrush x:Key="tvBackground" Stretch="UniformToFill" Opacity="1" ImageSource="pack://application:,,,/Resources/speelstuk.png"/>
17: <ImageBrush x:Key="tvBackgroundHighlight" Stretch="UniformToFill" Opacity="1" ImageSource="pack://application:,,,/Resources/speelstukHighlight.png"/>
18: <ImageBrush x:Key="itemHomeBackground" Stretch="UniformToFill" Opacity="1" ImageSource="pack://application:,,,/Resources/home.png"/>
19: </s:TagVisualization.Resources>
20:
21: <s:TagVisualization.Template>
22: <ControlTemplate TargetType="{x:Type local:BoardPiece}">
23: <Grid x:Name="mainGrid" Width="126" Height="142" Background="{StaticResource tvBackground}" />
24:
25: <ControlTemplate.Triggers>
26: <Trigger Property="IsAcceptingNewRelation" Value="true">
27: <Setter TargetName="mainGrid" Property="Background" Value="{StaticResource tvBackgroundHighlight}" />
28: </Trigger>
29: <Trigger Property="IsAcceptingNewRelation" Value="false">
30: <Setter TargetName="mainGrid" Property="Background" Value="{StaticResource tvBackground}" />
31: </Trigger>
32: </ControlTemplate.Triggers>
33: </ControlTemplate>
34: </s:TagVisualization.Template>
35:
36: </s:TagVisualization>

 



If anyone out there know how I can prevent VS from crashing (probably by pointing out what part of WPF I don’t understand here …) it’s much appreciated if you tell me :-)



Bye,



Bart