Monday, August 11, 2008

Bind with Expression Blend: cannot create Instance Exception

Databinding with XAML is a lot of work. There is no drag drop table wizard, don’t search for it. Expression Blend (2.5) have a little bit more automatic support for binding Objects. When Blend is in Design View there is under Project tab a Data tab.

Step1: open + CLR Object option- then you get a error message “loading assembly” which can be ignored (what else?)






In the next dialog the class have to be selected which returns the data.

In my case I got 100 times an error in Blend. The exception says nothing for me.










Step 2: Debugging

Expression Blend tries to create a instance of my object during design time. That's a risk cause many parameters can differ to run time like, connections string, database, path and so one.

First there must be some split between design and runtime code. There is a trick to do that. If no htmlpage is present, the container must be something else. Till now a usual container of silverlight xap applications is a browser (and nothing else)

Public Sub New() ' für Blend Designmode If HtmlPage.IsEnabled = False Then 'dummydaten mit vollem pfad Dim myxml = XDocument.Load("northwind.xml")

In this container is Blend. Then attach the Blend process to visual studio debugger. For that visual stuido must be open and the project must be compiled. Take Blend for compiling makes it a little bit easier for me. Dont forget to set breakpoint into your constructor.








In my case the code failes on the load place. The reason is ( now I know it) that i have added the XML file as content to my xap packet. But there is no xap package for blend.

The solution is to include the xml as resource into the assembly and load the xml with following code

Dim myxml = XDocument.Load("/databindingvb;component/northwindblend.xml")

Hope this will save you some hour.


No comments: