发布应用后,下载和保存文件失败

时间:2016-05-14 10:26:05

标签: c# visual-studio windows-phone-8 windows-store-apps

当我在Visual Studio 2013中完成Windows Phone 8上的应用程序开发并开始在我的物理设备上进行测试时 - 一切都很完美。 在Windows Mobile Store中发布应用程序后,当我在设备上下载应用程序时,它给我一些错误,我想下载并将文件保存到IsolatedStorage。这发生在这个片段中。

我错过了一些权限?为什么当我通过VS调试应用程序时一切正常,但是在发布后 - 失败了?

private Task<Stream> DownloadFile(Uri url)
    {
        var task = new TaskCompletionSource<Stream>();
        var webClient = new WebClient(); 
        webClient.OpenReadCompleted += (s, e) =>
        {
            if (e.Error != null) task.TrySetException(e.Error);
            else if (e.Cancelled) task.TrySetCanceled();
            else task.TrySetResult(e.Result);
        };
        webClient.OpenReadAsync(url);
        return task.Task;
    }

    private async Task<Problem> DownloadFileFromWeb(Uri uriToDownload, string fileName, CancellationToken cToken)
    {
        try
        {
            using (Stream stream = await DownloadFile(uriToDownload))
            using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (storage.FileExists(fileName)) return Problem.Other;
                using (IsolatedStorageFileStream file = storage.CreateFile(fileName))
                {
                    const int BUFFER_SIZE = 8192;
                    byte[] buf = new byte[BUFFER_SIZE];

                    int bytesread = 0;
                    while ((bytesread = await stream.ReadAsync(buf, 0, BUFFER_SIZE)) > 0)
                    {
                        cToken.ThrowIfCancellationRequested();
                        file.Write(buf, 0, bytesread);
                    }
                }
            }
            return Problem.Ok;
        }
        catch (Exception exc)
        {
            if (exc is OperationCanceledException)
                return Problem.Cancelled;
            else return Problem.Other;
        }
    }

0 个答案:

没有答案