我的应用程序不会删除隔离存储中加载页面上的保存文件。 delete和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>
答案 0 :(得分:0)
当我运行代码时,我在MainPage.xaml.cs的第33行得到一个NullReferenceException
这是因为您引用了this.LayoutRoot
的DataContext但从未设置过它。实际上,您已将DataContext设置为ContentPanel
因此,第32行应为:
ViewDiskModel model = this.ContentPanel.DataContext as ViewDiskModel;
此外:
视图模型不知道在使用属性的默认OneWay
绑定时已检查了哪些项目。您需要将其更改为TwoWay
,以便将UI中的更改反映回视图模型。
<CheckBox IsChecked="{Binding IsChecked, Mode=TwoWay}" Content="{Binding FileName}" />
通过这两项更改,将删除所选文件。
其他问题:
一个接一个地创建多个文件似乎存在一些问题,一次只创建一个,但我没有考虑过这个问题。
在使用实现Dispose()
的{{1}}时,您不是在调用using
(或使用IsolatedStorageFile
指令)。
代码表示即使没有选择或删除文件,它也删除了文件。
该应用尝试显示IsolatedStorage配额,但手机上不存在此概念。没有配额,这就是为什么它显示64位整数的最大值,而不管手机中磁盘的大小。
可能更多......?