XAML Announced Collection Hangs Silverlight

I played with declaring objects in XAML. I have these classes in my Silverlight assembly:

public class TextItem { public string TheValue { get; set; } } public class TextItemCollection { public ObservableCollection<TextItem> TextItems { get; set; } } 

Then I have this in my XAML:

 <UserControl.Resources> <app:TextItemCollection x:Key="TextItemsResource"> <app:TextItemCollection.TextItems> <app:TextItem TheValue="Hello world I am one of the text values"/> <app:TextItem TheValue="And I am another one of those text items"/> <app:TextItem TheValue="And I am yet a third!"/> </app:TextItemCollection.TextItems> </app:TextItemCollection> </UserControl.Resources> 

For some reason, if I turn on this node when I try to debug the application, Silverlight freezes (I just see a spinning blue loading circle). If I comment that node, it starts immediately.

Any ideas?

+4
source share
1 answer

Code Summary: The TextItems property is null. This cannot help the XAML parser.

According to experimental results: I get an exception when starting the application in the debugger (I use Silverlight 4):

 System.Windows.Markup.XamlParseException occurred Message=Collection property '__implicit_items' is null. [Line: 12 Position: 40] LineNumber=12 LinePosition=40 StackTrace: at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator) InnerException: 

You must initialize TextItems. You must also make the setter private so that others cannot ruin you. Try this, you should find that it works fine:

 public class TextItemCollection { public TextItemCollection() { TextItems = new ObservableCollection<TextItem>(); } public ObservableCollection<TextItem> TextItems { get; private set; } } 
+6
source

All Articles