将从Web服务接收的数据存储在windows phone 7应用程序的隔离存储中

时间:2014-02-11 05:42:47

标签: c# web-services windows-phone-7

我正在为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;
    }

2 个答案:

答案 0 :(得分:1)

好的,我知道你想要一些代码,所以我们在这里试试下面的代码:

public const string NewssXml = "Newss.xml";

public News()
{
    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 client = new KejriwalService.aapSoapClient();
        client.getarvindNewsCompleted += new EventHandler<KejriwalService.getarvindNewsCompletedEventArgs>(client_getarvindNewsCompleted);
        client.getarvindNewsAsync();

        progressName.Visibility = System.Windows.Visibility.Visible;
    }
}

//upon download completed, display data then save the xml to iso store
void client_getarvindNewsCompleted(object sender, KejriwalService.getarvindNewsCompletedEventArgs e)
{
    var doc = XDocument.Parse(e.Result);
    PopulateList(doc);
    WriteXml(doc);
}

private void PopulateList(XDocument doc)
{
    List<Newss> listData = new List<Newss>();

    progressName.Visibility = System.Windows.Visibility.Collapsed;

    foreach (var location in doc.Descendants("UserDetails"))
    {
        Newss data = new Newss();
        data.News_Title = location.Element("News_Title").Value;
        data.News_Description = location.Element("News_Description").Value;
        data.Date_Start = location.Element("Date_Start").Value;
        data.image_path = location.Element("image_path").Value;
        data.ImageBind = new BitmapImage(new Uri( @"http://political-leader.vzons.com/ArvindKejriwal/images/uploaded/"+data.image_path, UriKind.Absolute));
        listData.Add(data);
    }
    listBox1.ItemsSource = listData;
}

private XDocument ReadXml(out bool isSuccess) 
{ 
    isSuccess = false;
    var doc = new XDocument(); 
    using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication()) 
    { 
        try 
        { 
            if (store.FileExists(NewssXml)) 
            { 
                using (var sr = new StreamReader(new IsolatedStorageFileStream(NewssXml, 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(NewssXml, FileMode.Create, store))) 
            { 
                sw.Write(document.ToString()); 
            } 
        } catch (Exception ex) { return false; } 
    } 
    return true; 
}

只有你想知道这是如何构建的:

  • 一般步骤与我上个月在this answer中解释的相同。
  • ReadXmlWriteXml方法改编自this blog post
  • 其余部分将从您现有的代码
  • 重构

答案 1 :(得分:0)

将所有数据存储在XDocument中并检索是开销。而是使用您的数据库来存储和检索数据。这会减少您的代码工作。