我有两个页面:第一页是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" />
答案 0 :(得分:1)
我会使用IsolatedStorageSettings存储列表,并在单击按钮时将动态文本与isolatedstoragesettings中的列表进行比较。然后在FavouritesList页面上,将列表框的itemsource设置为IsolatedStorageSettings中的列表。这里是要遵循的步骤: 1.创建模型/类以设置文本块上显示的动态文本
public class favourites
{
public string myText { get; set; }
}
2。在MainPage.xaml.cs上的按钮单击事件中,如果需要,首先将动态文本(从哪里获取)到文本块,然后创建列表和/或比较
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;
}
我没有测试过这个,但绝对应该有效。希望它有所帮助!