【发布时间】:2014-10-22 21:50:21
【问题描述】:
似乎每个人都认为您必须在 Windows Phone 8 上使用独立存储,但我还没有找到为什么。我还使用了一些我正在移植的代码,传统的File.CreateText(Windows.ApplicationModel.Package.Current.InstalledLocation) 似乎可以正常工作。
所以在代码中,每个人似乎都在做this (from developer.nokia.com):
IsolatedStorageFile fileStorage = IsolatedStorageFile.GetUserStoreForApplication();
StreamWriter Writer = new StreamWriter(new IsolatedStorageFileStream("TestFile.txt", FileMode.OpenOrCreate, fileStorage));
Writer.WriteLine(textBox1.Text);
Writer.Close();
这实际上非常温顺。我已经看到太多的初学者教程使async 成为这样,并且无法弄清楚为什么。然而,上面的代码是在 WP7 上下文中呈现的。
更新:虽然以下代码在从 Visual Studio 运行时可在 WP8 (HTC 8XT) 和 WP8.1 (Lumia 640) 上运行,但当我部署到商店时,它立即炸毁了尝试保存到文件。
下面的代码似乎也能正常工作,至少在 WP 模拟器、运行 Windows Phone 8 的 HTC 8XT 和运行 WP 8.1 的 Lumia 640 上。下面的代码可以在稍微更好的上下文中看到at this link,但这是重要的东西。是的,我正在使用一些匈牙利语。对不起。显然,您的页面需要有一个名为
txtText 的 TextBox 和一个名为 strFileLoc 的全局变量。
Windows.ApplicationModel.Package package =
Windows.ApplicationModel.Package.Current;
Windows.Storage.StorageFolder installedLocation =
package.InstalledLocation;
this.strFileLoc = Path.Combine(installedLocation.Path,
"myFile.txt");
string strToWrite = this.txtText.Text;
using (StreamWriter sw = File.CreateText(this.strFileLoc))
{
sw.WriteLine(strToWrite);
sw.Close();
}
// Load
string strText = string.Empty;
if (File.Exists(this.strFileLoc))
{
using (StreamReader sr =
new StreamReader(File.OpenRead(this.strFileLoc)))
{
strText = sr.ReadToEnd();
}
}
else
{
strText = "File doesn't exist";
}
this.txtText.Text = strText;
这可以在生产应用中使用吗?为什么或为什么不?
【问题讨论】:
-
我知道。责备this guy 来自,拍摄,15 多年前。这是一个诅咒。 ;^) 至少它是一个示例应用程序。真正的代码有像
this.txtPrice.Text这样可怕的东西。 /sigh 我只为个人项目做,我发誓!
标签: c# windows-phone-8 windows-phone-8.1