繁体   English   中英

Windows Phone-隔离存储例外

[英]Windows Phone - Isolated Storage exception

下面的代码捕获用户选择的服务器路径,并保存在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

那就是抛出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);
}

那应该可以解决您的问题,并且可以正常工作:)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM