WEB开发网
开发学院图形图像Flash Silverlight 2 - Simple Editing of Web Service ... 阅读

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 min

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.

1 2 3 4  下一页

Tags:Silverlight Simple Editing

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接