我刚刚编写了一个代码,用于通过使用上下文菜单删除LongListSelector的项目(保存在独立存储中)。但我遇到了有关在隔离存储中删除文件的问题。有些文件是从隔离存储中删除的,但有些文件没有(当我退出并重新打开应用程序时它就消失了)
这是我的事件句柄" Delete_Click"
private void Delete_Click(object sender, RoutedEventArgs e)
{
MessageBoxResult message = MessageBox.Show("This recording will be deleted permanent.", "Delete!", MessageBoxButton.OKCancel);
if (message == MessageBoxResult.OK)
{
using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication())
{
var selectedSound = (sender as MenuItem).DataContext as SoundData;
SpeakerLongListSelector.ItemsSource.Remove(selectedSound);
// Delete selectedSound.FilePath
if (isoStore.FileExists(selectedSound.FilePath))
{
isoStore.DeleteFile(selectedSound.FilePath);
}
// Remove the SoundData from App.ViewModel.CustomSounds
App.ViewModel.CustomSounds.Items.Remove(selectedSound);
// Save the list of CustomSounds to IsolatedStorage.ApplicationSettings
var data = JsonConvert.SerializeObject(App.ViewModel.CustomSounds);
IsolatedStorageSettings.ApplicationSettings[SoundModel.CustomSoundKey] = data;
IsolatedStorageSettings.ApplicationSettings.Save();
if (!isoStore.FileExists(selectedSound.FilePath))
{
MessageBox.Show("This recording has been deleted successfully from your phone.");
}
else
MessageBox.Show("Sorry. Something went wrong.");
}
}