简体   繁体   中英

How to Save Items to IsolatedStorage and Populate a ListBox

I have a class that contains a few items that I need to save to IsolatedStorage as well as populate into a ListBox. The class looks as follows

public class History
{
    public string Network {get; set;} 

    public DateTime Date {get; set;} 
}

And I will be using it to populate a ListBox of items with Network and Date combinations (the Network being the type of network connection that was detected when a button click event occurs). I simply just want to display a history of items in a ListBox that displays the detected Network connection type and Date that the network was detected. This data will only be determined on a click event. My main question is, how can I save this data to IsolatedStorage and then populate each occurance to a ListBox to show a history of detected network connections and their respective dates?

You can SharpSerializer from nuget to do this.

here is how i did it

public static class IsolatedStorageOperations
    {
        public static async Task Save<T>(this T obj, string file)
        {
            await Task.Run(() =>
            {
                IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication();
                IsolatedStorageFileStream stream = null;

                try
                {
                    stream = storage.CreateFile(file);
                    var serializer = new SharpSerializer();
                    serializer.Serialize(obj,stream);
                }
                catch (Exception)
                {
                }
                finally
                {
                    if (stream != null)
                    {
                        stream.Close();
                        stream.Dispose();
                    }
                }
            });
        }

        public static async Task<T> Load<T>(string file)
        {

            IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication();
            T obj = Activator.CreateInstance<T>();

            if (storage.FileExists(file))
            {
                IsolatedStorageFileStream stream = null;
                try
                {
                    stream = storage.OpenFile(file, FileMode.Open);
                    var serializer = new SharpSerializer();

                    obj = (T)serializer.Deserialize(stream);
                }
                catch
                {
                }
                finally
                {
                    if (stream != null)
                    {
                        stream.Close();
                        stream.Dispose();
                    }
                }
                return obj;
            }
            await obj.Save(file);
            return obj;
        }
    }

here obj is the List instance, where object is the instance of your class History. This List will be the itemsource for ListBox

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