Reality80
Reality80

Reputation: 85

Windows Phone 8.1: Save list in app settings/storage

I just started developing a Windows Phone 8.1 app.

The app allows the user to add 'data' (strings) to his favorites-list. Now the question is:

What is the best way to save the list or the single entries of the list so I am able load these favorites again on the next app start?

I thought I should save the data to a (text-) file so I can read it line by line and put the list together again.

What do you think, is this the best way to handle something like this? I am new to the Windows Phone 8.1 platform and any help is really appreciated - thanks!

Upvotes: 3

Views: 3289

Answers (2)

Reality80
Reality80

Reputation: 85

I think i finally got it to work. Should thought about something like that much earlier.

So I got my async Task SaveAppSettingsAsync() and just call it in the Suspending-Event and in my Hardwarebuttons_Pressed-Event when the app closes:

private async void OnSuspending(object sender, SuspendingEventArgs e)
    {
        var deferral = e.SuspendingOperation.GetDeferral();


        await SaveAppSettingsAsync();


        deferral.Complete();
    }

private async void HardwareButtons_BackPressed(object sender, BackPressedEventArgs e)
    {
        Frame rootFrame = Window.Current.Content as Frame;
        if (rootFrame != null)
        {
            e.Handled = true;
            if (rootFrame.CurrentSourcePageType == typeof(MainPage))
            {
                await SaveAppSettingsAsync();

                this.Exit();
            }
            else if (rootFrame.CanGoBack)
                rootFrame.GoBack();
        }
    }

Thanks for helping. I think I got better at understanding how to handle async tasks now. As I said - I'm new to Windows Phone and never really used them before.

Upvotes: 2

Romasz
Romasz

Reputation: 29792

The best method depends on size of data and your needs. As for saving it in Settings you can try to make an array of string upon save and make list upon loading data. Simple example can look like this:

List<string> favourites = new List<string>();
protected void Method()
{
    // save as array
    ApplicationData.Current.LocalSettings.Values["myList"] = favourites.ToArray();

    // retrive your array and make a list from it
    favourites = ((string[])ApplicationData.Current.LocalSettings.Values["myList"]).ToList();
}

Remember that LocalSettings support only simple types and have other limits - for more information take a look at MSDN.

Upvotes: 2

Related Questions