问题
My App wouldn't delete the save files on the loading page in the isolated storage. The codes for the delete and the ViewDiskModel.cs class are below:
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>
回答1:
When I run the code I get a NullReferenceException
on line 33 in MainPage.xaml.cs
This is because you referenced the DataContext of this.LayoutRoot
but had never set this. You had in fact set the DataContext of ContentPanel
.
Line 32 should therefore be:
ViewDiskModel model = this.ContentPanel.DataContext as ViewDiskModel;
Additionally:
The view model was unaware of which items had been checked as you were using the default OneWay
binding of properties. You need to change this to TwoWay
so that changes in the UI are reflected back to the view model.
<CheckBox IsChecked="{Binding IsChecked, Mode=TwoWay}" Content="{Binding FileName}" />
With these two changes the selected files are deleted.
Other issues:
There appears to be some issue around the creation of multiple files one after the other such that only one gets created at a time, but I didn't look into this.
You are not calling
Dispose()
(or usingusing
directives) when working withIsolatedStorageFile
which implementsIDisposable
.The code says it has deleted the files even if there aren't any selected or to delete.
The app tries to display the IsolatedStorage quota but this concept doesn't exist on the phone. There is no quota whihc is why it shows the maximum value for a 64bit integer regardless of the size of the disk in the phone.
And possibly more...?
来源:https://stackoverflow.com/questions/6411090/viewdiskmodel-deleteselectedfiles-executenull-does-not-delete-any-files