Мое приложение не удалит сохраненные файлы на странице загрузки в изолированном хранилище. Коды для удаления и класса ViewDiskModel.cs приведены ниже:
LoadingPage.cs
private void button2_Click(object sender, RoutedEventArgs e)
{
ViewDiskModel model = lstBox1.DataContext as ViewDiskModel;
int m_iSelectedLoad = lstBox1.SelectedIndex;
if (m_iSelectedLoad >= 0)
{
model.DeleteSelectedFiles.Execute(null);
}
MessageBox.Show("Files Successfully Deleted");
}
ViewDiskModel.cs:
public class FileItem : ModelBase
{
public bool isChecked;
public bool IsChecked
{
get { return this.isChecked; }
set
{
this.isChecked = value;
this.OnPropertyChanged("IsChecked");
}
}
public string FileName { get; set; }
public string FileText { get; set; }
}
public class ViewDiskModel : ModelBase
{
private IsolatedStorageFile currentStore;
public IsolatedStorageFile Store
{
get
{
this.currentStore = this.currentStore ?? IsolatedStorageFile.GetUserStoreForApplication();
return this.currentStore;
}
}
private ObservableCollection<FileItem> _files;
public ObservableCollection<FileItem> Files
{
get
{
this._files = this._files ?? this.LoadFiles();
return this._files;
}
}
private ObservableCollection<FileItem> LoadFiles()
{
ObservableCollection<FileItem> files = new ObservableCollection<FileItem>();
foreach (string filePath in this.Store.GetFileNames())
files.Add(new FileItem { FileName = filePath });
return files;
}
private ICommand _deleteSelectedFiles;
public ICommand DeleteSelectedFiles
{
get
{
this._deleteSelectedFiles = this._deleteSelectedFiles ?? new DelegateCommand(this.OnDeleteSelected);
return this._deleteSelectedFiles;
}
}
private ICommand _readSelectedFiles;
public ICommand ReadSelectedFiles
{
get
{
this._readSelectedFiles = this._readSelectedFiles ?? new DelegateCommand(this.OnReadSelected);
return this._readSelectedFiles;
}
}
private void OnDeleteSelected()
{
IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication();
List<FileItem> removedItems = new List<FileItem>();
foreach (var item in this.Files)
{
if (item.IsChecked)
if (storage.FileExists(item.FileName))
{
storage.DeleteFile(item.FileName);
removedItems.Add(item);
}
}
foreach (var item in removedItems)
this.Files.Remove(item);
}
private void OnReadSelected()
{
IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication();
List<FileItem> removedItems = new List<FileItem>();
foreach (var item in this.Files)
{
if (item.IsChecked)
if (storage.FileExists(item.FileName))
{
storage.DeleteFile(item.FileName);
removedItems.Add(item);
}
}
foreach (var item in removedItems)
this.Files.Remove(item);
}
}
LoadingPage.XAML:
<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0" DataContext="{StaticResource vmDiskModel}">
<Button Content="Back" Height="72" HorizontalAlignment="Left" Margin="0,530,0,0" Name="button1" VerticalAlignment="Top" Width="160" Click="button1_Click" />
<Button Content="Delete" Height="72" HorizontalAlignment="Left" Margin="150,530,0,0" Name="button2" VerticalAlignment="Top" Width="160" Click="button2_Click" />
<Button Content="Continue" Height="72" HorizontalAlignment="Left" Margin="296,530,0,0" Name="button3" VerticalAlignment="Top" Width="160" Click="button3_Click" />
<TextBlock Height="30" HorizontalAlignment="Left" Margin="6,6,0,0" Name="textBlock1" Text="PLease select a save file to load." VerticalAlignment="Top" />
<ListBox ItemsSource="{Binding Files}" Margin="0,42,0,115" SelectionChanged="ListBox_SelectionChanged" Name="lstBox1" DataContext="{StaticResource vmDiskModel}">
<ListBox.ItemTemplate>
<DataTemplate >
<CheckBox IsChecked="{Binding IsChecked}" Content="{Binding FileName}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</Grid>