【问题标题】:Windows Phone - Isolated Storage exceptionWindows Phone - 独立存储异常
【发布时间】:2014-08-13 12:17:28
【问题描述】:

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

}

下一个代码(另一个屏幕),使用隔离存储中保存的服务器路径来创建一个 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”按钮,它可以工作。

抱歉英语不好

【问题讨论】:

  • 你能告诉我异常是在代码中的什么地方抛出的吗?是单击 Sinc 按钮时吗?因为我只复制了添加和检索的代码,它工作正常。顺便说一句,对不起,我之前的回答不正确。
  • 当我点击“sinc”按钮时抛出异常。
  • 您是否检查了来自 IsolatedStorageSettings 的值是否正确加载?您可以提供您存储的值作为服务器路径,以便我可以检查您的其余代码吗?
  • 您可以使用http://infassteste.url.ph。如果您存储文件路径并退出应用程序,然后从“isolatedstorage”返回并加载它,则可以正常工作。保存时和加载后立即出现问题。
  • 完成了。 :) 检查答案。

标签: c# windows-phone-8 isolatedstorage


【解决方案1】:

这与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);
}

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

【讨论】:

  • 如果我更改为Absolute,则生成异常:Invalid URI: The format of the URI could not be determined.
  • 你能不能在url = serverName + "/json.html"; 行放一个断点,检查serverName 是否有空值或者它有你提供给我的url。
  • 变量的值没问题。抱歉,我在 Windows 上使用 VS2013 express,我如何设置“断点”?感谢先进
  • 我提到的那一行,在左边那一行的正前方你可以看到行号(可能没有启用,如果是空的区域)点击它会出现红色圆圈,那是断点。调试应用程序,并在 IDE 停止在该行时观察 IDE,将鼠标悬停在 serverName 变量上以查看其中的值。
  • 哦,真的,变量“serv”是“null”。但我不明白,因为如果退出应用程序并返回,它可以工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-11-16
  • 1970-01-01
  • 2017-11-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多