单击按钮将文本块文本添加到收藏夹列表

Add textblock text to favorite list on button click

我有两个页面:第一个是 mainpage.xaml,第二个是 favoriteslist.xaml

mainpage.xaml 我有一个文本块,它会自动显示一些动态文本。

我在 mainpage.xaml 上也有一个按钮。

当我单击该按钮时,我希望从中显示在文本块上的文本应该转到 favoriteslist.xaml 页面中的收藏夹列表。

如果文本已被收藏,出现在文本块上的文本应在单击按钮时从收藏列表中删除。

所以最后我需要帮助来实现这个动态显示已经创建的功能文本块,但我只需要知道如何开发 add to favorite 功能。

文本块:

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

按钮:

<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 x:Name="FavoriteListBox" />

我会使用 IsolatedStorageSettings 来存储列表,并在单击按钮时将动态文本与 isolatedstoragesettings 中的列表进行比较。然后在FavouritesList页面,将listbox的itemsource设置为IsolatedStorageSettings.So中的list,步骤如下: 1.创建一个model/class来设置在文本块

上显示的动态文本
public class favourites
{
    public string myText { get; set; }
}

2。在 MainPage.xaml.cs 上的按钮单击事件中,如果需要,首先将动态文本(从哪里获取)设置到文本块,然后创建列表 and/or 比较

 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();
    }       

现在剩下的就是在 FavouritesList 页面中显示始终更新的列表。我添加了一个 'NoData' 文本块,当列表中没有任何内容时,它应该是可见的。否则将显示列表。 在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"/>

在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;
        }

我还没有测试过这个,但肯定可以。希望对您有所帮助!