2

how I can saving List to settings in C# uwp?

define class for object

//class    

public class ListOfFile
        {
            public string File_DisplayName { get; set; }
            public string File_Name { get; set; }
            public string File_path { get; set; }
            public string File_Extension { get; set; }
            public StorageFile File_Storage { get; set; }
            public ImageSource File_Thumbnail { get; set; }
        }

define method For loadfile Method:

//method
void loadfile()
{
    List<ListOfFile> FileBind=new List<ListOfFile>();
     var FilesInFolder = await FolderItem.GetFilesAsync();

    foreach(var FileItem in FilesInFolder)
    {
           FileBind.Add(new ListOfFile()
                        {
                            File_DisplayName = FileItem.DisplayName,
                            File_Extension = FileItem.FileType
                        ,
                            File_Name = FileItem.Name
                        ,
                            File_path = FileItem.Path
                        ,
                            File_Thumbnail = bitmapimage
                        ,
                            File_Storage =FileItem
                        });
    }

    ApplicationData.Current.LocalSettings.Values["allfiles"]=FileBind;
}

end line Error :(

how I can saveing list in Settings? help me please.

3 Answers3

6

UWP Community Toolkit - Object Storage

I know this is over half a year since the original question was asked, but I wish I had found out about the UWP toolkit sooner, as I had a similar situation where I wanted to quickly and easily store a simple class list of device information to roam in roaming settings. Posting this here in case it might be use to someone else in a similar situation like the OP and myself.

If your application targets Windows 10 Build 10586 or higher, you can use the Object Storage helpers that are part of the UWP Community Toolkit by Microsoft to store small or large objects, which also includes Lists.

You can add the toolkit by adding the Microsoft.Toolkit.Uwp NuGet package to your UWP project.

Example for LocalObjectStorageHelper (Snippet Source)

var helper = new LocalObjectStorageHelper();

// Read simple objects
string keySimpleObject = "simple";
if (helper.KeyExists(keySimpleObject))
{
    string result = helper.Read<string>(keySimpleObject);
}

// Save simple objects
helper.Save(keySimpleObject, 47);

// Read complex/large objects 
string keyLargeObject = "large";
if (await helper.FileExistsAsync(keyLargeObject))
{
    var result = await helper.ReadFileAsync<MyLargeObject>(keyLargeObject);
}

// Save complex/large objects 
var o = new MyLargeObject
{
    ...
};
await helper.SaveFileAsync(keySimpleObject, o);

Same can be done with RoamingObjectStorageHelper.

Important Notes

You can save both simple and complex objects like lists using Save(key, object) and SaveFileAsync(key, object).

Save will store the object in local/roaming settings (depending on the storage helper you pick); This means it is subject to data constraints and the total serialized object size must be less than 8 KB.

SaveFileAsync will store the object as a serialized file in the LocalState or RoamingState directories of your app (at least, I believe so) to be accessed later using ReadFileAsync. Use this if your object is very complex and you expect might serialize into a size greater than 8 KB.

Important Caveat

Be careful when storing large/complex objects or list of objects using SaveFileAsync. Each UWP app is only allotted 100 KB (AKA the RoamingStorageQuota). If the quota is exceeded, data replication will cease.

Community
  • 1
  • 1
Arctic
  • 325
  • 3
  • 9
2

Don't forget that ApplicationData.LocalSettings has limitations.

Each setting can be up to 8K bytes in size.

You are trying to store

List<T> 

in settings. Take a look at supported types

List<T> 

is missing there. Looks like you can't do it this way. There is might be only with some converting to object

Take a look at solution on this similar topic: How to store a list of objects in application settings

UPDATE:

You can try replace BinaryFormatter with DataContractSerializer. Here is nice article that might help you: Saving and Loading App Data (Windows Store C# UWP / 8.1)

Community
  • 1
  • 1
Alexej Sommer
  • 2,677
  • 1
  • 14
  • 25
0

You should not be storing entire files and bitmap images to settings. That is not the correct use of an app's settings feature. At the most, you can store a list of files and their paths, which may allow you to read the contents and display a thumbnail at run-time. If you had mentioned exactly what it is you're trying to achieve, then maybe we could help you a little better.

If you absolutely need to store entire files within your app, you can either copy those files into your app's local storage folder or you can look into integrating with OneDrive for a more portable solution.

aalaap
  • 4,145
  • 5
  • 52
  • 59