我的代码无法读取文本文件。显然; y,当我想阅读文件时,他们给了我一个“找不到文件!”它位于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!");
}
}
}
}
}
答案 0 :(得分:0)
您发布的代码运行正常。您的问题必须在应用程序逻辑的其他位置。在您的读取逻辑中,在执行ReadSelectedFiles命令之前,您似乎没有设置FileName属性。您的代码需要设置此代码,因为这是它尝试查找的文件名。除非你在其他地方设置这个,否则这可能是问题的原因。
所以也许像
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
AddFileModel model = this.LayoutRoot.DataContext as AddFileModel;
model.FileName = "myFile.dat";
model.ReadSelectedFiles.Execute(null);
}