0

I have two pages: the first is mainpage.xaml and the second is favoriteslist.xaml.

In mainpage.xaml I have a text block, which shows some dynamic text automatically.

And I have a button also on mainpage.xaml.

From which I want when I click on that button, text appears on text block should go to favorite list in favoriteslist.xaml page.

If text already favorite, which text appears on text block should be removed from favorite list on button click.

So finally I need help to implement this functionality textblock which shows dynamically already created but I only need to know how to develop add to favorite functionality.

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)
{
}

Listbox:

<ListBox x:Name="FavoriteListBox" />
krlzlx
  • 5,752
  • 14
  • 47
  • 55
Shubham Sahu
  • 1,963
  • 1
  • 17
  • 34

1 Answers1

1

I would use IsolatedStorageSettings to store the list and compare the dynamic text to the list in the isolatedstoragesettings upon button click. Then on FavouritesList page, set itemsource of the listbox to the list in IsolatedStorageSettings.So here are the steps to be followed: 1. Create a model/class to set the dynamic text being shown on the text block

public class favourites
{
    public string myText { get; set; }
}

2. In the button click event on MainPage.xaml.cs, first set the dynamic text (where ever you are getting it from) to the text block if you need to and then create the list and/or compare

 private void AddToFavoritesButton_Click(object sender, RoutedEventArgs e)
    {
        //your dynamic text set to textblock
        StringTextBlock.Text = myDynamicText;  

        //Set value of your text to member variable of the model/class
        favourites f = new favourites();
        f.myText = myDynamicText;

        IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;

        /*Check if "FavouritesList" key is present in IsolatedStorageSettings
          which means already a list had been added. If yes, retrieve the
          list, compare each item with your dynamic text, add or remove
          accordingly and replace the new list in IsolatedStorageSettings
          with same key. */

        if (settings.Contains("FavouritesList"))
        {
            List<favourites> l = (List<favourites>)settings["FavouritesList"];
            for(int i = 0; i <= l.Count()-1; i++)
            {
                if (l[i].Equals(myDynamicText))
                {
                    l.RemoveAt(i);
                    settings["FavouritesList"] = l;
                }
                else
                {
                    l.Add(f);
                    settings["FavouritesList"] = l;
                }
            }           
        }

        //If no key in IsolatedStorageSettings means no data has been added
        //in list and IsolatedStorageSettings. So add new data

        else
        {
            List<favourites> l = new List<favourites>();
            l.Add(f);
            settings["FavouritesList"] = l;
        }
        settings.Save();
    }       

Now all that is left is show the always updated list in the FavouritesList Page. I added a 'NoData' textblock that should be visible when there is nothing in the list. Else the list will be displayed. In FavouritesList.xaml

 <ListBox x:Name="FavoriteListBox" Visibility="Collapsed">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding myText}"/>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

        <TextBlock Name="NoData" 
                   Text="No Data" 
                   Visibility="Collapsed" 
                   Width="50" 
                   Height="50"/>

In FavouritesList.xaml.cs

 IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
        if (settings.Contains("FavouritesList"))
        {
           List<favourites> l = (List<favourites>)settings["FavouritesList"];
            if(l.Count!= 0)
            {
                NoData.Visibility = System.Windows.Visibility.Collapsed;
                FavoriteListBox.Visibility = System.Windows.Visibility.Visible;
                FavoriteListBox.ItemsSource = l;
            }                  
        }

        else
        {
            FavoriteListBox.Visibility = System.Windows.Visibility.Collapsed;                   
            NoData.Visibility = System.Windows.Visibility.Visible;
        }

I have not tested this but should definitely work. Hope it helps!

user5434084
  • 149
  • 1
  • 8
  • okay let me check, if work definitely upvote your answer. thank's – Shubham Sahu Nov 13 '16 at 06:32
  • all things working correct but when i favorite 1st text it go to favorite list in favoriteslist.xaml but when i try to favorite next text application stops or freeze – Shubham Sahu Nov 13 '16 at 07:26
  • Have you tried debugging the app? Where is the problem occuring – user5434084 Nov 13 '16 at 11:16
  • Debugging continue without any exception or error, but app get freeze or terminated when i add one more favorite – Shubham Sahu Nov 13 '16 at 17:02
  • Have you atleast tried to search about the exception? Because i found quite much on it with a simple search. I have provided the whole code. The error is not about the code.its about the data you are storing which is quite big. So pls upvote for my answer. And here is reference to your problem. Pls try the solutions and work on it abit – user5434084 Nov 14 '16 at 12:35
  • http://stackoverflow.com/questions/8563933/c-sharp-out-of-memory-exception. Thanks – user5434084 Nov 14 '16 at 12:35
  • Okay, i will check, but you have any idea why this error (as show in image) 1st favorite can be add easily but when try to add another favorite this exception occurs. e.g in a text block show 123abc then i click on favorite, text go to isolated storage and available in favoritelist.xaml but in second time app through exception. – Shubham Sahu Nov 15 '16 at 05:32