Shubham Sahu Shubham Sahu - 18 days ago 6
C# Question

Add to favorites in windows phone

I have a text block in my app, which will show different text per the user's preference.

I want the user to be able to save their favorite that text, which will be saved after a button click. Later, the list of saved favorite text is to be displayed as a selection option. Does any body have an idea as to how to create this process? Please provide a code example.

Textblock:

<TextBlock x:Name="StringTextBlock" Text="" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}" />


Button:

<Button Grid.Row="2" x:Name="AddToFavoritesButton"
Content="Add" Style="{StaticResource ButtonStyle2}" Margin="2"
Click="AddToFavoritesButton_Click"/>


C#

private void AddToFavoritesButton_Click(object sender, RoutedEventArgs e)
{
}


and a list box on another page which can be accessed later on click on favorites button

<ListBox x:Name="FavoriteListBox" />


I would like to know how to add the current text from a text block to the list of favorite text. Or, another means to add to the list of favorite text.

Answer

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 :-) .

Comments