MVC-REST-SilverLight 之 ViewModels\MainViewModel.cs
Posted on 2012-02-21 21:07 如是如是 阅读(203) 评论(0) 编辑 收藏 举报using System.ComponentModel;
using RestExample.Model;
using System.Collections.ObjectModel;
using System;
using System.Net;
using System.Diagnostics;
using System.Xml;
using System.Xml.Linq;
using System.Linq;
using System.Collections.Generic;
using System.Threading;
using System.IO;
namespace RestClient.ViewModels
{
public class MainViewModel : INotifyPropertyChanged
{
private SynchronizationContext _context;
public MainViewModel()
{
_context = SynchronizationContext.Current;
}
private Customer _selectedCustomer;
public Customer SelectedCustomer
{
get { return _selectedCustomer; }
set
{
_selectedCustomer = value;
NotifyPropertyChanged("SelectedCustomer");
}
}
private ObservableCollection<Customer> _customers =
new ObservableCollection<Customer>();
public ObservableCollection<Customer> Customers
{
get { return _customers; }
}
public void AddNewLocalCustomer()
{
var customer = new Customer();
_customers.Add(customer);
SelectedCustomer = customer;
}
public void LoadCustomers()
{
string url = "http://localhost:9090/api/customers";
Uri uri = new Uri(url, UriKind.Absolute);
var request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = "GET";
request.Accept = "text/xml";
request.BeginGetResponse((result) =>
{
var response = request.EndGetResponse(result);
var stream = response.GetResponseStream();
if (stream != null)
{
var reader = XmlReader.Create(stream);
XElement set = XElement.Load(reader);
_context.Post((o) =>
{
IEnumerable<Customer> customers =
from XElement item in set.Descendants("Customer")
select new Customer()
{
ID = int.Parse(item.Element("ID").Value),
FirstName = item.Element("FirstName").Value,
LastName = item.Element("LastName").Value,
Balance = decimal.Parse(item.Element("Balance").Value)
};
Customers.Clear();
foreach (Customer customer in customers)
{
Customers.Add(customer);
}
}, null);
}
}, null);
}
public void DeleteSelectedCustomer()
{
}
public void UpdateSelectedCustomer()
{
if (SelectedCustomer != null)
{
string url = "http://localhost:9090/api/customers";
Uri uri = new Uri(url, UriKind.Absolute);
var request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = "POST";
request.ContentType = "text/xml";
request.BeginGetRequestStream((result) =>
{
var req = (HttpWebRequest)result.AsyncState;
var stream = req.EndGetRequestStream(result);
if (stream != null)
{
string customerTemplate =
"<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
"<Customer>" +
"<ID>{0}</ID>" +
"<LastName>{1}</LastName>" +
"<FirstName>{2}</FirstName>" +
"<Balance>{3}</Balance>" +
"</Customer>";
string xml = string.Format(customerTemplate,
SelectedCustomer.ID,
SelectedCustomer.LastName,
SelectedCustomer.FirstName,
SelectedCustomer.Balance);
Debug.WriteLine("POST: " + xml);
using (var writer = new StreamWriter(stream))
{
writer.Write(xml);
writer.Close();
}
req.BeginGetResponse((requestResult) =>
{
var req2 = (HttpWebRequest)requestResult.AsyncState;
var response = req2.EndGetResponse(requestResult);
var responseStream = response.GetResponseStream();
var reader = new StreamReader(responseStream);
string responseText = reader.ReadToEnd();
Debug.WriteLine("Response: " + responseText);
}, req);
}
}, request);
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this,
new PropertyChangedEventArgs(propertyName));
}
}
}