ViewDiskModel.DeleteSelectedFiles.Execute(null) 不删除任何文件

发布于 2024-11-16 09:35:26 字数 5293 浏览 3 评论 0原文

我的应用程序不会删除隔离存储中加载页面上的保存文件。删除和 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>

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

金橙橙 2024-11-23 09:35:26

当我运行代码时,我在 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}" />

通过这两项更改,所选文件将被删除。

其他问题:

  • 在一个接一个地创建多个文件时似乎存在一些问题,以至于一次只能创建一个文件,但我没有研究这一点。

  • 使用实现 IDisposableIsolatedStorageFile 时,您没有调用 Dispose() (或使用 using 指令) >.

  • 代码表示,即使没有任何选定或要删除的文件,它也已删除文件。

  • 应用程序尝试显示IsolatedStorage配额,但手机上不存在此概念。没有配额,这就是为什么无论手机中的磁盘大小如何,它都会显示 64 位整数的最大值。

  • 可能还有更多...?

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 using using directives) when working with IsolatedStorageFile which implements IDisposable.

  • 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...?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文