Windows Phone -隔离存储异常
本文关键字:存储 异常 隔离 Phone Windows | 更新日期: 2023-09-27 18:18:00
下面的代码捕获用户选择的服务器路径并保存在IsolatedStorage上:
private void ConfSalvar(object sender, RoutedEventArgs e)
{
IsolatedStorageSettings iso = IsolatedStorageSettings.ApplicationSettings;
if (iso.Contains("isoServer"))
{
iso["isoServer"] = server.Text;
}
else
{
iso.Add("isoServer", server.Text);
}
}
下一个代码(另一个屏幕),使用保存在IsolatedStorage上的服务器路径来创建URL:
IsolatedStorageSettings iso = IsolatedStorageSettings.ApplicationSettings;
if (iso.TryGetValue<string>("isoServer", out retornaNome))
{
serv = retornaNome;
}
private void sinc(object sender, EventArgs e)
{
order.Visibility = Visibility.Collapsed;
client = new WebClient();
url = serv + "/json.html";
Uri uri = new Uri(url, UriKind.RelativeOrAbsolute);
client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted);
client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
client.OpenReadAsync(uri);
}
private void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
string strFileName = url.Substring(url.LastIndexOf("/") + 1, (url.Length - url.LastIndexOf("/") - 1));
IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication();
// Path Storage
// *** If File Exists
if (isoStore.FileExists(strFileName))
{
isoStore.DeleteFile(strFileName);
}
IsolatedStorageFileStream dataFile = new IsolatedStorageFileStream(strFileName, FileMode.CreateNew, isoStore);
long fileLen = e.Result.Length;
byte[] b = new byte[fileLen];
e.Result.Read(b, 0, b.Length);
dataFile.Write(b, 0, b.Length);
dataFile.Flush();
object lenghtOfFile = dataFile.Length;
dataFile.Close();
order.Visibility = Visibility.Visible;
ProgressBar1.Visibility = Visibility.Collapsed;
MessageBox.Show("Arquivo salvo!");
}
private void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
ProgressBar1.Visibility = Visibility.Visible;
this.ProgressBar1.Value = e.ProgressPercentage;
}
所以,如果我保存了文件路径,然后立即点击"sinc"按钮,就会出现一个异常,在操作过程中出现异常,使结果无效。查看InnerException获取异常详细信息。但是如果我保存文件路径并关闭应用程序,打开应用程序并点击"sinc"按钮,它就可以工作了。
不好意思
这与IsolatedStorageSettings
无关。它工作得很好。问题是当您创建Uri
时,您将UriKind
设置为RelativeOrAbsolute
。
这就是抛出异常和InnerException状态的原因,This operations is not supported for a relative URI
。您需要做的是将UriKind
更改为Absolute
。所以代码块应该是这样的:
private void sinc(object sender, EventArgs e)
{
if (iso.TryGetValue<string>("isoServer", out retornaNome))
{
serv = retornaNome;
}
else
{
// Let the user know.
return;
}
order.Visibility = Visibility.Collapsed;
client = new WebClient();
url = serv + "/json.html";
Uri uri = new Uri(url, UriKind.Absolute); // <<< Absolute instead of RelativeOrAbsolute
client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted);
client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
client.OpenReadAsync(uri);
}
这应该可以解决你的问题,并正常工作:)