关于从新页面上的独立存储中读取文本文件

时间:2011-07-04 11:15:49

标签: c# windows-phone-7 isolatedstorage

当我想从隔离存储中读取文本时,我不确定这组代码是否有用(我将从类文件中粘贴所有内容,只是因为我错过了任何内容。):< / p>

ViewDiskModel.cs:

  namespace WindowsPhoneApplication1.Model
{
    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 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> readItems = new List<FileItem>();
            foreach (var item in this.Files)
            {
                if (item.IsChecked)
                    if (storage.FileExists(item.FileName))
                    {
                        storage.DeleteFile(item.FileName);
                        readItems.Add(item);
                    }
            }

            foreach (var item in readItems)
                this.Files.Remove(item);
        }


    }
}

如果我错了,请纠正我。我知道可能有一种更简单的方法,但文本将显示在另一页上。并且有比它看起来更多的代码。如果需要,请随意让我添加更多。

以下是AddFileModel类

AddFileModel.cs:

  namespace WindowsPhoneApplication1.Model
    {
        public class AddFileModel : ModelBase
        {

            public bool text;
            public bool Text
            {
                get { return this.text; }
                set
                {
                    this.text = value;
                    this.OnPropertyChanged("Text");
                }
            }




            private string _filename;
            public string FileName
            {
                get
                {
                    return this._filename;
                }
                set
                {
                    this._filename = value;
                    this.OnPropertyChanged("FileName");
                }

            }

            private string _filetext1;
            public string FileText1
            {
                get
                {
                    return this._filetext1;
                }
                set
                {
                    this._filetext1 = value;
                    this.OnPropertyChanged("FileText1");
                }

            }

            private string _filetext2;
            public string FileText2
            {
                get
                {
                    return this._filetext2;
                }
                set
                {
                    this._filetext2 = value;
                    this.OnPropertyChanged("FileText2");
                }
            }

            private string _filetext3;
            public string FileText3
            {
                get
                {
                    return this._filetext3;
                }
                set
                {
                    this._filetext3 = value;
                    this.OnPropertyChanged("FileText3");
                }


            private string _rdbtext1;
            public string RdbText1
            {
                get
                {
                    return this._rdbtext1;
                }
                set
                {
                    this._rdbtext1 = value;
                    this.OnPropertyChanged("RdbText1");
                }


            }

            private bool _rdb1;
            public bool Rdb1
            {
                get
                {
                    return this._rdb1;
                }
                set
                {
                    this._rdb1 = value;
                    this.OnPropertyChanged("Rdb1");
                }


            }

            private ICommand _saveFile;
            public ICommand SaveFile
            {
                get
                {
                    this._saveFile = this._saveFile ?? new DelegateCommand(this.OnSaveFile);
                    return this._saveFile;
                }
            }

             private ICommand _readSelectedFiles;
            public ICommand ReadSelectedFiles
            {
                get
                {
                    this._readSelectedFiles = this._readSelectedFiles ?? new DelegateCommand(this.OnReadSelected);
                    return this._readSelectedFiles;
                }
            }

            private void OnSaveFile()
            {
                if (!string.IsNullOrEmpty(this.FileName))
                {
                    using (var store = IsolatedStorageFile.GetUserStoreForApplication())
                    {
                        if (store.FileExists(FileName))
                            store.DeleteFile(FileName);

                        using(StreamWriter writer = new StreamWriter(new IsolatedStorageFileStream(FileName, FileMode.OpenOrCreate, store)))
                        {
                            writer.WriteLine(this.FileText1 + this.FileText2 + this.FileText3 + this.RdbText1 + this.Rdb1 );
                            writer.Close();
                        }
                    }
                }
            }

            private void OnReadSelected()
            {
                if (!string.IsNullOrEmpty(this.FileName))
                {
                    using (var store = IsolatedStorageFile.GetUserStoreForApplication())
                    {
                        if (store.FileExists(FileName))

                            using (IsolatedStorageFileStream fileStream = store.OpenFile(FileName, FileMode.Open, FileAccess.Read))
                            {
                                using (StreamReader reader = new StreamReader(fileStream))
                                {
                                    string textData = this.FileText1 + this.FileText2 + this.FileText3 + this.RdbText1 + this.Rdb1;
                                    textData = reader.ReadLine();
                                }
                            }
                        else
                        {
                            MessageBox.Show("File not found!");
                        }
                    }
                }
            }
        }
    }

0 个答案:

没有答案