1

我编写了一个简单的函数来将文件从 SkyDrive 下载到 IsolatedStorage。

    public static async Task<T> DownloadFileData<T>( string fileID, string filename )
        {
        var liveClient = new LiveConnectClient( Session );

        // Download the file
        await liveClient.BackgroundDownloadAsync( fileID + "/Content", new Uri( "/shared/transfers/" + filename, UriKind.Relative ) );

        // Get a reference to the Local Folder
        string root = ApplicationData.Current.LocalFolder.Path;
        var storageFolder = await StorageFolder.GetFolderFromPathAsync( root + @"\shared\transfers" );

        // Read the file
        var FileData = await StorageHelper.ReadFileAsync<T>( storageFolder, filename );
        return FileData;
        }

该函数无法运行该行:

// Download the file
await liveClient.BackgroundDownloadAsync( fileID + "/Content", new Uri( "/shared/transfers/" + filename, UriKind.Relative ) );

出现错误:“在 mscorlib.ni.dll 中发生‘System.InvalidOperationException’类型的异常,但未在用户代码中处理

请求已经提交”

如果我将行修改为(删除等待),则该函数成功:

// Download the file
liveClient.BackgroundDownloadAsync( fileID + "/Content", new Uri( "/shared/transfers/" + filename, UriKind.Relative ) );

这是为什么?

谢谢

4

1 回答 1

1

需要检查 BackgroundTransferService.Request 是否为空,如果没有则删除任何待处理的请求。

我像这样修改了我的代码,它似乎工作正常:

public static async Task<T> DownloadFileData<T>( string skydriveFileId, string isolatedStorageFileName )
    {
    var liveClient = new LiveConnectClient( Session );

    // Prepare for download, make sure there are no previous requests
    var reqList = BackgroundTransferService.Requests.ToList();
    foreach ( var req in reqList )
        {
        if ( req.DownloadLocation.Equals( new Uri( @"\shared\transfers\" + isolatedStorageFileName, UriKind.Relative ) ) )
            {
            BackgroundTransferService.Remove( BackgroundTransferService.Find( req.RequestId ) );
            }
        }

    // Download the file into IsolatedStorage file named @"\shared\transfers\isolatedStorageFileName"
    try
        {
        await liveClient.BackgroundDownloadAsync( skydriveFileId + "/Content", new Uri( @"\shared\transfers\" + isolatedStorageFileName, UriKind.Relative ) );
        }
    catch ( TaskCanceledException exception )
        {
        Debug.WriteLine( "Download canceled: " + exception.Message );
        }

    // Get a reference to the Local Folder
    var storageFolder = await GetSharedTransfersFolder<T>();

    // Read the file data
    var fileData = await StorageHelper.ReadFileAsync<T>( storageFolder, isolatedStorageFileName );
    return fileData;
    }
于 2013-07-17T19:46:05.427 回答