Silverlight:在 XAML 中声明数据集合? [英] Silverlight: Declaring a collection of data in XAML?

查看:25
本文介绍了Silverlight:在 XAML 中声明数据集合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的 Silverlight for Windows Phone 7 应用程序中声明一些数据.我不确定语法是什么.

I would like to declare some data in my Silverlight for Windows Phone 7 application. I'm not sure what the syntax is.

例如:

public class Person 
{
      public string Name {get; set;}
      public int Age {get; set;}
}

<Application.Resources>
    <Data x:Name="People">
         <Person Age="2" Name="Sam" />
         <!-- ... -->
    </Data>
</Application.Resources>

显然 Data 不是一个有效的标签.我在这里想要什么?

Obviously Data is not a valid tag. What do I want here?

推荐答案

首先你需要定义一个容器类型:-

You will need to define a container type first of all:-

using System.Collections.ObjectModel;

...

public class People : ObservableCollection<Person> { }

然后,您需要将 People/Person 类所在的命名空间添加到 Xaml 中,通常如下所示:-

You then need to add the namespace that your People/Person classes are present in to the Xaml typicall this would look like:-

<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:local="clr-namespace:SilverlightApplication1"
         x:Class="SilverlightApplication1.App"
         >

只需将SilverlightApplication1"替换为您的应用程序命名空间即可.

Just replace "SilverlightApplication1" with your application namespace.

现在你可以:-

     <Application.Resources>
         <People x:Name="People">
             <Person Age="2" Name="Sam" />
             <Person Age="11" Name="Jane" />
         </People>
     </Application.Resources>

这篇关于Silverlight:在 XAML 中声明数据集合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆