我一直在玩XAML中声明对象.我在Silverlight程序集中有这些类:
public class TextItem
{
public string TheValue { get; set; }
}
public class TextItemCollection
{
public ObservableCollection<TextItem> TextItems { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
然后,我在我的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>
Run Code Online (Sandbox Code Playgroud)
出于某种原因,如果我在尝试调试应用程序时包含该节点,Silverlight会挂起(我只看到旋转的蓝色加载圆圈).如果我注释掉该节点,它会立即运行.
有任何想法吗?
By code review: Your TextItems property is null. That can't help the XAML parser.
By experimental results: I get an exception when running the app in the debugger (I'm using 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:
Run Code Online (Sandbox Code Playgroud)
You should initialize TextItems. You should also make the setter private so others can't mess you up. Try this, you should find it works fine:
public class TextItemCollection
{
public TextItemCollection()
{
TextItems = new ObservableCollection<TextItem>();
}
public ObservableCollection<TextItem> TextItems { get; private set; }
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2079 次 |
| 最近记录: |