【问题标题】:Silverlight local storage equivalent in HTML 5在 HTML 5 中等效的 Silverlight 本地存储
【发布时间】:2014-09-28 13:36:29
【问题描述】:

我想将配置存储在每个设备(而不是每个浏览器)的客户端计算机上,并在所有支持 HTML 5 的浏览器中访问它。我可以使用 Silverlight 的 Isolated Storage 存储此配置,所有带有此插件的浏览器都可以读取它。我看到(我的理解)以下 HTML 5 技术的缺点

本地存储:Data will be deleted if user clears cache
索引数据库:Data won't be shared across browsers
文件 API:Can't read any arbitrary file without permission of user. 用户可能必须拖放文件才能读取它,我将在文件中存储一些配置,所以我不希望用户拖放文件每次我想阅读配置

是否有任何项目可以帮助我,或者我可以在这种情况下使用一些“设计模式”?
我应该查看 HTML 5 之外的内容吗?

经历了以下没有成功: https://softwareengineering.stackexchange.com/questions/156682/how-to-move-silverlight-app-to-html5 https://github.com/Modernizr/Modernizr/wiki/HTML5-Cross-browser-Polyfills http://www.manasinc.com/silverlight-and-html5-comparison/

【问题讨论】:

    标签: html silverlight local-storage indexeddb fileapi


    【解决方案1】:

    如果用户清除缓存,则无法持久保存数据,甚至接近的唯一方法是将数据存储在服务器端(与用户配置文件相关),然后在应用程序连接和发送参数时(如用户代理和屏幕资源)将特定配置重新下载到缓存中。

    此外,我怀疑是否有一种方法可以实现与每个 Web 浏览器的完全兼容(与大多数情况一样),但是就存储而言,Web 存储是与当前浏览器最兼容的方法。 http://caniuse.com/#feat=namevalue-storage

    【讨论】:

      【解决方案2】:
      // To save (Silverlight Out of browser)
      // This code is used when you have a Silvelight runs out of browser and typical local storage won't work, you can create an xml file saved on local c drive and retrieve that data when restarting the app.
      
      XDocument doc = new XDocument(
                      new XElement("Root",
                          new XElement("userinit", "john doe")
                      ));
      string path = "C:\\Windows\\Temp\\setup.xml";
      using (StreamWriter writetext = new StreamWriter(path))
      {
          doc.Save(writext);
      }    
      
      // to read, read the same file you save to the saved setup
      string path = "C:\\Windows\\Temp\\setup.xml";
      //<?xml version="1.0" encoding="utf-8"?>
      //<Root>
      //    <userinit>john doe</userinit>
      //</Root>
      // from the xml you can extract your saved data
      string result = sting.Empty;
      if (File.Exists(path))
      {
          System.IO.StreamReader read = new StreamReader(path);
          result = read.ReadToEnd();
      }
      

      【讨论】:

      • 我上面的回答是针对浏览器外的 Silverlight 应用程序。
      【解决方案3】:
      // for in browser app
      // to save 
      // you can create a local storage file in the form of xml
      // this file will persist as long as you don't clear the browsing history
      XDocument doc = new XDocument(
                  new XElement("Root",
                      new XElement("userinit", "john doe")
                  ));
      using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication())
      {
          using (IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream("setup.xml", FileMode.Create, isoStore))
          {
              doc.Save(isoStream);
          }
      }
      // to read
      // the saved xml will be
      //<Root><userinit>john doe</userinit></Root>
      // you can extract data from there
      // 
      string lineOfData = String.Empty;
      using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
      {
          if (!isf.FileExists("setup.xml"))
              return;
           using (IsolatedStorageFileStream isfs = new IsolatedStorageFileStream("setup.xml", FileMode.Open, isf))
              {
                  using (StreamReader sr = new StreamReader(isfs))
                  {
      
                      lineOfData  = sr.ReadToEnd();
                  }
             }
       }
      
      // get the data from xml
      string initials;
      XDocument xdoc = XDocument.Parse(lineOfData);
      var q = (from c in xdoc.Descendants("userinit")
                           select c).Single();
                  _Initial = q.Value;
                  initials = _Initial;
      

      【讨论】:

      • 请对该代码添加一些解释,以便其他人可以从中学习
      猜你喜欢
      • 2012-03-21
      • 2011-06-04
      • 1970-01-01
      • 1970-01-01
      • 2011-09-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-04
      相关资源
      最近更新 更多