ViewDiskModel.DeleteSelectedFiles.Execute(null)不会删除任何文件
本文关键字:删除 任何 文件 DeleteSelectedFiles Execute null ViewDiskModel | 更新日期: 2023-09-27 17:59:45
My App不会删除独立存储中加载页面上的保存文件。删除和ViewDiskModel.cs类的代码如下:
加载Page.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);
}
}
加载页面.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>
当我运行代码时,我在MainPage.xaml.cs的第33行得到一个NullReferenceException
这是因为您引用了this.LayoutRoot
的DataContext,但从未设置过。事实上,您已经设置了ContentPanel
的DataContext
因此,第32行应为:
ViewDiskModel model = this.ContentPanel.DataContext as ViewDiskModel;
此外:
视图模型不知道检查了哪些项,因为您使用了属性的默认OneWay
绑定。您需要将其更改为TwoWay
,以便将UI中的更改反映回视图模型。
<CheckBox IsChecked="{Binding IsChecked, Mode=TwoWay}" Content="{Binding FileName}" />
通过这两项更改,选定的文件将被删除。
其他问题:
一个接一个地创建多个文件,一次只创建一个文件,这似乎存在一些问题,但我没有对此进行研究。
使用实现
IDisposable
的IsolatedStorageFile
时,您没有调用Dispose()
(或使用using
指令)。代码说它已经删除了文件,即使没有任何选择或要删除的文件。
该应用程序试图显示IsolatedStorage配额,但手机上不存在此概念。没有配额,这就是为什么无论手机中的磁盘大小如何,它都会显示64位整数的最大值。
还有可能更多。。。?