This depends a lot on whether you want to store the "favorites" just for the running session of the app or over multiple launches.
If you just want to store them temporarily, you could create a class that would hold a List<string>
for the favorites and then on the "other page" load them in a ObservableCollection<string>
and use data binding to display them. An introduction into data binding is here - https://msdn.microsoft.com/en-us/windows/uwp/data-binding/data-binding-quickstart
To preserve the favorites, you will need to save them somewhere, preferably in a file. The easiest way to do that would be to create a file in ApplicationData.Current.LocalFolder
and serialize the data into its contents, and reload them again during the next app start. A great serialization library working with JSON files is JSON.NET. It is very easy to serialize data into a JSON string and back. You can then use FileIO.WriteTextAsync()
and FileIO.ReadTextAsync()
methods to write and read the data to and from the file.
Simple example
On the first page you could have a TextBox
and a Button
. When the user types something in the TextBox
and clicks the Button
, you can do something like this:
FavoritesManager.AddAsync( inputBox.Text );
The FavoritesManager
class could look roughly like this:
public static class FavoritesManager
{
List<string> _favorites = null;
public static async Task LoadFromStorageAsync()
{
_favorites =
JsonConvert.DeserializeObject<List<string>>(
await FileIO.ReadAllTextAsync( "somefile.txt" ) );
}
public static async Task AddAsync( string text )
{
_favorites.Add( text );
await FileIO.WriteAllTextAsync( "somefile.txt",
JsonConvert.SerializeObject( _favorites ) );
}
public static IEnumerable<string> GetFavorites()
{
return _favorites;
}
}
You will have to call the LoadFromStorageAsync
method before you try to get the favorites so that they are ready in the _favorites
list.
On the second page you will just call GetFavorites
to retrieve the favorites and then store them in a ObservableCollection<string>
and use data binding to bind them to the list control :-) .