Select to view content in your preferred language

Application shutdown without error event-Undocumented change in Silverlight 4.0

650
3
05-24-2010 05:33 AM
AvronPolakow
Occasional Contributor
After upgrading my application to Silverlight 4.0 and making all the necessary changes as documented, a persistent error occurred, which caused my application to shut down without any error message. Even the debugger couldn't help because a routine was completed succesfully and then the application hanged without any warning or error.

It took a lot of effort to discover that there has been an undocumented change in Silverlight4.0 where the x:Name of an object in the XAML cannot itself be bound. I replaced the x:Name attribute with the Tag attribute and the bug was solved. I don't know if this was an intentional change on MS's part.

--- This is wrong in Silverlight 4.0
<TextBox  x:Name="{Binding FieldName}"  Text="{Binding FieldData}" Visibility="{Binding FieldVisibility}" Grid.Column="1" Width="120" FontSize="9" KeyUp="DataKeyUp" />

+++ It should be replaced with
<TextBox  Tag="{Binding FieldName}"  Text="{Binding FieldData}" Visibility="{Binding FieldVisibility}" Grid.Column="1" Width="120" FontSize="9" KeyUp="DataKeyUp" />
0 Kudos
3 Replies
dotMorten_esri
Esri Notable Contributor
This was intentional, since this is not really valid XAML and doesn't make much sense either.
x:Name is only used to be able to get to the element in codebehind, but if it keeps changing through the binding, what's the point?
0 Kudos
AvronPolakow
Occasional Contributor
Even if it didn't make sense before earlier versions of Silverlight didn't throw an exception.
At least in Silverlight 4.0 there should have been some error thrown.

I was using x:Name binding in templates (eg in listboxes and trees) where some form of identification was needed to identify separate rows (a la XML attributes).

It just seemed natural to use the x:Name for each row. The Tag attribute I replaced it with is OK except that I was using that for other information about rows in some templates where I now have to parse out the Tag in the application to get to the row name.

I could have used a collapsed (hidden) textblock but using the Name seemed just the most intuitive.
0 Kudos
dotMorten_esri
Esri Notable Contributor
It sounds like what you really need to do is build a proper viewmodel. I suggest you research the MVVM pattern a bit.
0 Kudos