簡體   English   中英

在Windows Phone 7應用程序的隔離存儲中存儲從Web服務接收的數據

[英]storing data received from web service in isolated storage in windows phone 7 application

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

好的,我知道您想要一些代碼,所以我們開始嘗試以下代碼:

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; 
}

僅當您想知道它是如何構建的時:

  • 一般步驟與我上個月在此答案中解釋的步驟相同。
  • 這篇博客文章改編的ReadXmlWriteXml方法
  • 其余的從您現有的代碼中重構

將所有數據存儲在XDocument中並進行檢索是開銷。 而是使用數據庫存儲和檢索數據。 這將減少您的代碼工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM