Я не уверен, сработает ли этот набор кодов, когда я захочу прочитать текст из изолированного хранилища (я собираюсь вставить все из файла класса, только если я что-то упустил.):
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!");
}
}
}
}
}
}