How to store ListBox items into IsolatedStorage in WindowsPhone 8 c#

How to store ListBox items into IsolatedStorage in WindowsPhone 8 c#

Advertisemen

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 { getset; } 
        public string Location { getset; } 
    } 
    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

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"); 
            } 
        }
8)ScreenShots 



  





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






Have a nice day by 

Advertisemen

Disclaimer: Gambar, artikel ataupun video yang ada di web ini terkadang berasal dari berbagai sumber media lain. Hak Cipta sepenuhnya dipegang oleh sumber tersebut. Jika ada masalah terkait hal ini, Anda dapat menghubungi kami disini.

Tidak ada komentar:

Posting Komentar

© Copyright 2017 Tutorial Unity 3D