【问题标题】:storing data received from web service in isolated storage in windows phone 7 application将从 Web 服务接收到的数据存储在 Windows Phone 7 应用程序的隔离存储中
【发布时间】: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


    【解决方案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 中解释的相同。
    • ReadXml 和 WriteXml 方法改编自 this blog post
    • 其余部分从您现有的代码中重构

    【讨论】:

    • 有一个小错误。错误是:名称 SaveXml 在当前上下文中不存在
    • 我正在上传一个代码,我试图将数据存储在不同页面的独立存储中。在这里,我只是将收到的数据存储在文本块中。我在这里收到一个错误:“名称 e 在当前上下文中不存在”。
    • 我收到以下错误:private void PopulateList(XDocument doc) { var data = e.Result;
    • 很明显,PopulateList 中没有定义e 仔细查看我回答中的示例。也许尝试用doc替换xml。
    • 我应该只在 populatelist 函数中进行更改吗?
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多