【发布时间】:2014-02-11 15:18:52
【问题描述】:
我正在为 windows phone 7 构建一个应用程序,我需要从 web 服务中提取数据并将其显示在我的应用程序中。现在,每次调用数据来自 Web 服务的页面时,都会一次又一次地调用 Web 服务,因此显示数据需要时间。所以我想将数据存储在隔离的存储中,这样就不会每次都调用 Web 服务。请帮助我做到这一点。我调用 Web 服务的代码是:
public const string aboutxml = "about.xml";
public about()
{
InitializeComponent();
LoadData();
}
private void LoadData()
{
bool isSuccess;
//try to load data from iso store
var doc = ReadXml(out isSuccess);
if (isSuccess) PopulateList(doc);
//if failed (data doesn't exists in iso store), download data from web service
else
{
KejriwalService.aapSoapClient myclient = new KejriwalService.aapSoapClient();
myclient.getarvindAboutCompleted += new EventHandler<KejriwalService.getarvindAboutCompletedEventArgs>(myclient_getarvindAboutCompleted);
myclient.getarvindAboutAsync();
progressName.Visibility = System.Windows.Visibility.Visible;
}
}
//upon download completed, display data then save the xml to iso store
void myclient_getarvindAboutCompleted(object sender, KejriwalService.getarvindAboutCompletedEventArgs e)
{
var doc = XDocument.Parse(e.Result);
PopulateList(doc);
WriteXml(doc);
}
private void PopulateList(XDocument doc)
{
var data = e.Result;
XElement xml = XElement.Parse(data);
aboutview.Text = xml.Elements("UserDetails").Elements("about_details").First().Value;
progressName.Visibility = System.Windows.Visibility.Collapsed;
}
private XDocument ReadXml(out bool isSuccess)
{
isSuccess = false;
var doc = new XDocument();
using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
{
try
{
if (store.FileExists(aboutxml))
{
using (var sr = new StreamReader(new IsolatedStorageFileStream(aboutxml, FileMode.OpenOrCreate, store)))
{
doc = XDocument.Load(sr);
}
isSuccess = true;
}
}
catch (Exception ex) { }
}
return doc;
}
private bool WriteXml(XDocument document)
{
using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
{
try
{
using (var sw = new StreamWriter(new IsolatedStorageFileStream(aboutxml, FileMode.Create, store)))
{
sw.Write(document.ToString());
}
}
catch (Exception ex) { return false; }
}
return true;
}
【问题讨论】:
标签: c# web-services windows-phone-7