[英]Add textblock text to favorite list on button click
I have two pages: the first is mainpage.xaml
and the second is favoriteslist.xaml
. 我有两个页面:第一个是
mainpage.xaml
,第二个是favoriteslist.xaml
。
In mainpage.xaml
I have a text block, which shows some dynamic text automatically. 在
mainpage.xaml
我有一个文本块,它会自动显示一些动态文本。
And I have a button also on mainpage.xaml
. 我在
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. 当我单击该按钮时,我希望从哪个文本框中显示的文本应转到
favoriteslist.xaml
页面中的favoriteslist.xaml
夹列表。
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. 因此,最后我需要帮助来实现该功能文本块,该文本块动态显示已创建的内容,但我只需要知道如何开发
add to favorite
功能。
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# C#
private void AddToFavoritesButton_Click(object sender, RoutedEventArgs e)
{
}
Listbox: 列表框:
<ListBox x:Name="FavoriteListBox" />
I would use IsolatedStorageSettings to store the list and compare the dynamic text to the list in the isolatedstoragesettings upon button click. 单击按钮后,我将使用IsolatedStorageSettings来存储列表,并将动态文本与isolatedstoragesettings中的列表进行比较。 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
然后在FavouritesList页面上,将列表框的itemsource设置为IsolatedStorageSettings中的列表。因此,请按照以下步骤操作:1.创建一个模型/类以设置要在文本块上显示的动态文本。
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 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();
}
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.
我添加了一个“ NoData”文本块,当列表中没有任何内容时,该文本块应该可见。 Else the list will be displayed.
否则将显示列表。 In FavouritesList.xaml
在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 在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!
希望能帮助到你!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.