Мои коды не читают текстовый файл.Видимо; у, когда я хочу прочитать файл, они дали мне «Файл не найден!»окно сообщения, которое находится в цикле if else, указывающее, что код в цикле if не работает.Так что мне нужна помощь с этим.И этот вопрос до сих пор остается без ответа.Давая столько кодов ниже, я просто надеюсь, что это может помочь вам понять, в чем моя проблема и здесь, потому что я не очень хорош в программировании, и я просто пытаюсь сотрудничать.Извините тем, кого я много беспокоил.Все ответы, которые вы, гайз, дали, что-то значат, и я действительно ценю помощь.
CreateTextPage XAML:
<TextBox Margin="0,217,0,338" TextWrapping="Wrap" Text="{Binding FileName, Mode=TwoWay}" HorizontalAlignment="Right" Width="480" IsReadOnly="False" Name="textFileName" />
<TextBox Text="{Binding FileText1, Mode=TwoWay}" HorizontalAlignment="Left" Width="447" Height="378" VerticalAlignment="Top" Name="text1" />
<TextBox Text="{Binding FileText2, Mode=TwoWay}" HorizontalAlignment="Left" Width="447" Height="378" VerticalAlignment="Top" Name="text2"/>
CreateTextPage:
private void Button_Click(object sender, RoutedEventArgs e)
{
AddFileModel model = this.LayoutRoot.DataContext as AddFileModel;
model.SaveFile.Execute(null);
model.FileName = string.Empty;
model.FileText1 = string.Empty;
model.FileText2 = string.Empty;
MessageBox.Show("File saved successfully");
NavigationService.Navigate(new Uri("/CompleteQuestionPage.xaml", UriKind.Relative));
}
ReadFilePage XAML:
<TextBlock HorizontalAlignment="Right" Margin="0,0,-409,-260" TextWrapping="Wrap" VerticalAlignment="Bottom" Width="333" Text="{Binding FileName, Mode=TwoWay}" Name="titleText" />
<TextBlock TextWrapping="Wrap" Height="378" Width="452" Text="{Binding FileText1, Mode=TwoWay}" Name="textBlocky1" />
<TextBlock TextWrapping="Wrap" Height="378" Width="452" Text="{Binding FileText2, Mode=TwoWay}" Name="textBlocky2" />
ReadFilePage:
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
AddFileModel model = this.LayoutRoot.DataContext as AddFileModel;
model.ReadSelectedFiles.Execute(null);
}
Ниже приведен файл классов, отвечающий за метод.
Файл класса AddFileModel:
namespace WindowsPhoneApplication1.Model
{
public class AddFileModel : ModelBase
{
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 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 (var fileStream = store.OpenFile(FileName, FileMode.Create, FileAccess.Write))
{
using (var writer = new StreamWriter(fileStream))
{
writer.WriteLine(FileName);
writer.WriteLine(FileText1);
writer.WriteLine(FileText2);
}
}
}
}
}
private void OnReadSelected()
{
using (var store = IsolatedStorageFile.GetUserStoreForApplication())
{
if (store.FileExists(FileName))
{
using (var fileStream = store.OpenFile(FileName, FileMode.Open, FileAccess.Read))
{
using (var reader = new StreamReader(fileStream))
{
FileName = reader.ReadLine();
FileText1 = reader.ReadLine();
FileText2 = reader.ReadLine();
}
}
}
else
{
MessageBox.Show("File not found!");
}
}
}
}
}