簡體   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