我的window phone 7应用程序中有一个名为addFavorites的页面。 它显示一个人员列表,其中包含复选框和页面底部的按钮。
在复选框的已选中和未选中的事件上,我只是添加相应的对象并将其删除到列表。
点击添加按钮后,我将填充的列表添加到隔离存储。
以下是我的代码
public partial class AddFavourites : PhoneApplicationPage
{
public int checkedPlayers;
public List<MarathonPlayer> selectedPlayers = null;
public AddFavourites()
{
InitializeComponent();
if (IsolatedStorageSettings.ApplicationSettings.Contains("FavouritePlayers"))
{
selectedPlayers = (List<MarathonPlayer>)(IsolatedStorageSettings.ApplicationSettings["FavouritePlayers"]);
}
else
{
selectedPlayers = new List<MarathonPlayer>();
}
listOfPlayers.ItemsSource = ManageFavourites.listSearch;
}
private void checkBox_Checked(object sender, RoutedEventArgs e)
{
MarathonPlayer mp = (sender as CheckBox).DataContext as MarathonPlayer;
if (!IsolatedStorageSettings.ApplicationSettings.Contains("FavouritePlayers"))
{
selectedPlayers.Add(mp);
}
else
{
Boolean PlayerIsSelected = false;
selectedPlayers = null;
selectedPlayers = (List<MarathonPlayer>)(IsolatedStorageSettings.ApplicationSettings["FavouritePlayers"]);
for (int j = 0; j < selectedPlayers.Count(); j++)
{
PlayerIsSelected = false;
if (selectedPlayers[j].playerBibNum.Equals(mp.playerBibNum))
{
CheckBox cb = sender as CheckBox;
cb.IsChecked = false;
PlayerIsSelected = false;
MessageBox.Show("The runner is already added to your favourites");
break;
}
else if (selectedPlayers.Count == 5)
{
CheckBox cb = sender as CheckBox;
cb.IsChecked = false;
PlayerIsSelected = false;
MessageBox.Show("You cannot add more than 5 runners in your favourites");
break;
}
PlayerIsSelected = true;
}
if (PlayerIsSelected)
{
selectedPlayers.Add(mp);
}
}
}
private void checkBox_Unchecked(object sender, RoutedEventArgs e)
{
MarathonPlayer mp = (sender as CheckBox).DataContext as MarathonPlayer;
selectedPlayers.Remove(mp);
}
private void addFav_Click_1(object sender, RoutedEventArgs e)
{
if (IsolatedStorageSettings.ApplicationSettings.Contains("FavouritePlayers"))
{
IsolatedStorageSettings.ApplicationSettings.Remove("FavouritePlayers");
}
IsolatedStorageSettings.ApplicationSettings["FavouritePlayers"] = selectedPlayers;
IsolatedStorageSettings.ApplicationSettings.Save();
this.NavigationService.Navigate(new Uri("/Race Events/Favourites/ManageFavourites.xaml", UriKind.Relative));
}
}
问题:
当我加载应用程序时,我选中了一个复选框并按下后退按钮,表现不错。
然后我选择了一个项目并单击了添加按钮,并根据需要将数据添加到隔离存储中。
再次在隔离存储中添加一个项目后,我选中了一个复选框并单击了一个后退按钮,这次所选项目被添加到隔离存储中而不按下后退按钮,这意味着,现在ifind 2项目处于隔离存储中。
每次我只选中一个复选框并按下后退按钮,该项目就会被添加到隔离存储中,而不会按添加Butto。
我试图找出最新情况但无法找到答案。
请帮助其紧急
谢谢和问候
修改 我发现更多的是如果勾选复选框并取消选中它没有这样的问题,所以我认为按下后退按钮后,所有内容都被发送到隔离存储。
答案 0 :(得分:1)
正如@abhinav所提到的,您正在使用对存储在隔离存储中的列表的引用。因此,对列表所做的任何更改都会反映到隔离存储中。
您可以复制该列表:
selectedPlayers = new List<MarathonPlayer>((List<MarathonPlayer>)(IsolatedStorageSettings.ApplicationSettings["FavouritePlayers"]));
答案 1 :(得分:0)
selectedPlayers = (List<MarathonPlayer>)(IsolatedStorageSettings.ApplicationSettings["FavouritePlayers"]);
selectedPlayers
是对IsolatedStorageSettings
Dictionary元素的引用。所以所有更改都直接发送到您的IsolatedStorageSettings
。
尝试使用
IsolatedStorageSettings mysettings = IsolatedStorageSettings.ApplicationSettings;
希望这有帮助。