Silverlight 2 - Simple Editing of Web Service Data in a DataGrid
2008-10-24 11:46:57 来源:WEB开发网Somebody asked how you use the DataGrid in Silverlight 2 in order to load data from a web service, modify it and submit it back so I thought I'd spend 5 minutes experimenting with that.
I wrote a very simple web service with WCF;
[DataContract]
public class Person
{
[DataMember]
public int Id { get; set; }
[DataMember]
public string FirstName { get; set; }
[DataMember]
public string LastName { get; set; }
[DataMember]
public int Age { get; set; }
}
[ServiceContract]
public interface IPeopleService
{
[OperationContract]
List<Person> GetPeople();
[OperationContract]
void UpdatePeople(List<Person> inserts, List<Person> updates,
List<Person> deletes);
}
and implemented it in the simplest possible way ( no thread-safety, no concurrency );
public class PeopleService : IPeopleService
{
static PeopleService()
{
people = new Dictionary<int, Person>();
people.Add(1, new Person() { Id = 1, FirstName = "Mike", LastName = "Taulty", Age = 18 });
people.Add(2, new Person() { Id = 2, FirstName = "Mike", LastName = "Ormond", Age = 78 });
people.Add(3, new Person() { Id = 3, FirstName = "Daniel", LastName = "Moth", Age = 101});
}
public List<Person> GetPeople()
{
return (people.Values.ToList());
}
public void UpdatePeople(List<Person> inserts, List<Person> updates, List<Person> deletes)
{
if (inserts != null)
{
foreach (Person p in inserts)
{
p.Id = nextId++;
people.Add(p.Id, p);
}
}
if (deletes != null)
{
foreach (Person p in deletes)
{
people.Remove(p.Id);
}
}
if (updates != null)
{
foreach (Person p in updates)
{
Person updateEntry = people[p.Id];
updateEntry.FirstName = p.FirstName;
updateEntry.LastName = p.LastName;
updateEntry.Age = p.Age;
}
}
}
private static Dictionary<int, Person> people;
private static int nextId = 4;
}
and I made sure that the web.config file that had been edited by the tool when I inserted the WCF service had basicHttpBinding rather than the wsHttpBinding you get by default.
Tags:Silverlight Simple Editing
编辑录入:爽爽 [复制链接] [打 印]- ››silverlight全屏显示图片
- ››Silverlight MVVM 模式(一) 切近实战
- ››Silverlight for Windows Phone 7开发系列(1):...
- ››Silverlight for Windows Phone 7开发系列(2):...
- ››Silverlight for Windows Phone 7开发系列(3):...
- ››Silverlight for Windows Phone 7开发系列(4):...
- ››Silverlight for Symbian
- ››Silverlight3系列(四)数据绑定 Data Binding 1
- ››Simple Cloud API:编写可移植的、可互操作的云应...
- ››silverlight2 游戏 1 你能坚持多少秒
- ››Silverlight开发实践--PicZoomShow
- ››Silverlight自定义控件开发 - 令人懊恼的OnApplyT...
更多精彩
赞助商链接