下面的代码捕获用户选择的服务器路径并保存在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”按钮,它可以正常工作。
抱歉英语不好
答案 0 :(得分:1)
这与IsolatedStorageSettings
无关。它工作正常。问题是当您创建Uri
时,将UriKind
设置为RelativeOrAbsolute
。
是什么引发了Exception和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);
}
这应该可以解决您的问题,并且工作正常:)