简体   繁体   中英

How to store data in Isolated Storage in Windows Phone 7

I am building an app for windows phone 7.
I am using data which is coming from the web service which i want to save in isolated storage so that next time when the data is viewed it shows the data even when offline.

My cs file is:

public about()
{
    InitializeComponent();

    KejriwalService.aapSoapClient myclient = new KejriwalService.aapSoapClient();
    myclient.getarvindAboutCompleted += new EventHandler<KejriwalService.getarvindAboutCompletedEventArgs>(myclient_getarvindAboutCompleted);
    myclient.getarvindAboutAsync();        
}

void myclient_getarvindAboutCompleted(object sender, KejriwalService.getarvindAboutCompletedEventArgs e)
{
    var data = e.Result;

    XElement xml = XElement.Parse(data);

    aboutview.Text = xml.Elements("UserDetails").Elements("about_details").First().Value;
}

One of the easiest way to store data is to store them in the IsolatedStorageSettings. You can make a property for this purpose:

        string PropertyName
        {
            get
            {
                var settings = System.IO.IsolatedStorage.IsolatedStorageSettings.ApplicationSettings;
                if (settings.Contains("valueKeyName"))
                    return (string)settings["valueKeyName"];
                else
                    return null;
            }
            set
            {
                var settings = System.IO.IsolatedStorage.IsolatedStorageSettings.ApplicationSettings;
                if (settings.Contains("valueKeyName"))
                    settings["valueKeyName"] = value;
                else
                    settings.Add("valueKeyName", value);
            }
        }

IsolatedStorageSettings store pairs key-value in the dictionary. You should just choose key name of your storing value and name of the property.

with the help ofIsolatedStorageSettings you could save your data in the form of key value paire like this:

 var Iso_settings = System.IO.IsolatedStorage.IsolatedStorageSettings.ApplicationSettings;   
if (!Iso_settings.Contains("yourDataKey"))
    {
      Iso_settings.Add("yourDataKey", yourDataValue);
      Iso_settings.Save()//This will save your data in isolated storage.
    }

Don't go for Sqlite if you don't want offline data and to store Data two best method is to use Isolated Storage and using linq so if you have only 1-2 data members and you have to change there values then you can use Isolated Storage otherwise go for linq if you have 2-3 columns and rows to store data in form of Tables.
Isolated storage is used like this:

try
{
    if (IsolatedStorageSettings.ApplicationSettings.Contains("email_id"))
    {
        IsolatedStorageSettings.ApplicationSettings["email_id"] = emailid;
        IsolatedStorageSettings.ApplicationSettings["password"] = password;
        IsolatedStorageSettings.ApplicationSettings.Save();
    }
    else
    {
        IsolatedStorageSettings.ApplicationSettings.Add("email_id", emailid);
        IsolatedStorageSettings.ApplicationSettings.Add("password", password);
        IsolatedStorageSettings.ApplicationSettings.Save();
    }
}
catch (Exception ex)
{
    Console.WriteLine(ex.InnerException);
}

For linq http://www.codeproject.com/Articles/43025/A-LINQ-Tutorial-Mapping-Tables-to-Objects go for the link.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM