Introduction
In every mobile app,there is requirement for storing data and accessing data. We need to store data in our Phones to persist data when the application is not running.However fortunatley Windows Phone 7 &windows phone 8 provides a secure way to store data into Isolated Store.Isolated Storage, as the name suggests is a special virtualized file system that every application can access for standard IO operations yet the file is unavailable to any other application. Hence the files stored by one application is isolated from another.Note For beginners:
1)Isolated storage are best and simple for storing data,for more info you may visit
http://www.geekchamp.com/tips/all-about-wp7-isolated-storage--intro-to-isolated-storage
2)We can also store and access data using sqlite,i will be introduce it laterely or next article.
Source File at :IsolatedStorageList
Building the Sample
1)You will have to add a reference to System.Xml.Serialization
2)Needed <tt>System.IO.IsolatedStorage</tt> namespace to access files and/or application settings.
Description
1)However in this sample i had taken "MyData" class as well as "MyDataList"
C#
public class MyData
{
public string Name { get; set; }
public string Location { get; set; }
}
public class MyDataList : List<MyData>//for storing mydata class items with type of list
{
}
2) Add item to "MyDataList" like this way
C#
MyDataList listobj = new MyDataList();
listobj.Add(new MyData { Name = "subbu", Location = "hyd" });
listobj.Add(new MyData { Name = "Venky", Location = "Kadapa" });
listobj.Add(new MyData { Name = "raju", Location = "Kuwait" });
listobj.Add(new MyData { Name = "balu", Location = "US" });
listobj.Add(new MyData { Name = "gopi", Location = "London" });
listobj.Add(new MyData { Name = "rupesh", Location = "USA" });
listobj.Add(new MyData { Name = "ram", Location = "bang" });
3)Write list or listbox items into IsolatedStorage
C#
IsolatedStorageFile Settings1 = IsolatedStorageFile.GetUserStoreForApplication();
if (Settings1.FileExists("MyStoreItems"))
{
Settings1.DeleteFile("MyStoreItems");
}
using (IsolatedStorageFileStream fileStream = Settings1.OpenFile("MyStoreItems", FileMode.Create))
{
DataContractSerializer serializer = new DataContractSerializer(typeof(MyDataList));
serializer.WriteObject(fileStream, listobj);
}
4)Read
list or listbox items from IsolatedStorage
list or listbox items from IsolatedStorage
C#
if (Settings1.FileExists("MyStoreItems"))
{
using (IsolatedStorageFileStream fileStream = Settings1.OpenFile("MyStoreItems", FileMode.Open))
{
DataContractSerializer serializer = new DataContractSerializer(typeof(MyDataList));
listobj = (MyDataList)serializer.ReadObject(fileStream);
}
}
5)Remove list or listbox items from IsolatedStorage
C#
if (Settings1.FileExists("MyStoreItems"))
{
Settings1.DeleteFile("MyStoreItems");
MessageBox.Show("Items removed successfully.");
}
6)Binding listbox items from IsolatedStorage list data
C#
if (Settings1.FileExists("MyStoreItems"))
{
using (IsolatedStorageFileStream fileStream = Settings1.OpenFile("MyStoreItems", FileMode.Open))
{
DataContractSerializer serializer = new DataContractSerializer(typeof(MyDataList));
listobj = (MyDataList)serializer.ReadObject(fileStream);
}
}
Isolistbox.ItemsSource = listobj;//binding isolated storage list data
7)Write selected listbox class item into IsolatedStorage
C#
private void Isolistbox_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
{
MyData selecteddata = (MyData)Isolistbox.SelectedItem;//get listbox item data
//Write selected class item value into isolated storage
if (selecteddata != null)
{
if (Settings1.FileExists("MySelectedStoreItem"))
{
Settings1.DeleteFile("MySelectedStoreItem");
}
using (IsolatedStorageFileStream fileStream = Settings1.OpenFile("MySelectedStoreItem", FileMode.Create))
{
DataContractSerializer serializer = new DataContractSerializer(typeof(MyData));
serializer.WriteObject(fileStream, selecteddata);
}
MessageBox.Show("Your selected item Name:" + selecteddata.Name.ToString()+" " + "is stored in isolated storage");
}
}
Windows Phone tutorials for beginners key points
This section is included for only windows phone beginners.However this article can covers following questions.Off course these key words are more helpful for getting more best results about this topic in search engines like google/bing/yahoo..1. IsolatedStorage in windowsphone 8 c#
2. Storing ListBox items in isolatedstorage in windowsphone
3. Removing items from isolatedstorage in windowsphone
4. Reading listbox items from isolated storage
5. Binding listbox items from isolated storag
6. Storing listbox selcted item in isolatedstorage
7.How to get listbox selected item from isolated storage
when on restart of application and then on click of write listbox items previous items are lost.Please fix this.
ReplyDeleteThank you so much and keep it up,i fixed it in source code.Please see and download sample from here.
DeleteHow to delete a particular item from listbox items.
ReplyDeletelistboxobj.Items.RemoveAt(indexitem);
Deletei tried but it says "Operation not supported on read-only collection"
ReplyDeletether? need help, exception is thrown, i think observablecollection should be used but i do not about that.I need it.
ReplyDeleteone more thing you need to fix. when you write listitems thats working,then read listitems thats working but when you click on remove listitems it says items removed,but without exiting application if you write into listitems,previous items are not deleted,they are added to latest items.This only works when you click remove listitems and exit application, then you can see items are deleted.If you are not exiting application then it is not going to work.Please fix this issue and how to overcome exception "Operation not supported on read-only collection"
ReplyDeleteThank you so much for your deep involvement of my post.I updated source code and please download it and let me know :)
DeleteYou done it but i need help on deleting individual items,i used contextmenu style for deleting,each time when i try to delete a particular item it throws "Operation not supported on read-only collection".I used Isolistbox.items.remove(Isolistbox.selectedIndex);
DeleteHow can I store a single selected item? I have a list picker, and I would select an item from the list. I want this to be stored. When the app is launched again, the selection should be intact.
ReplyDelete