【问题标题】:OneDrive file only uploads when app is terminatedOneDrive 文件仅在应用程序终止时上传
【发布时间】:2014-12-30 14:21:01
【问题描述】:

我在将文件从通用应用程序上传到 OneDrive 时遇到问题,我无法理解或弄清楚如何调试。我使用this guide 完成了获取文件 ID 等的过程,直到几个小时前它都运行良好。

现在我可以获取文件夹和文件 ID,因此我假设我仍然成功连接到 OneDrive,并且我的互联网连接仍然有效。但是,当我进入 BackgroundUploadAsync 线程或之前正在执行的任何内容时,它永远不会返回。在下面的代码中,“Uploading new file to OneDrive...”消息永远不会消失。

奇怪的是,在上传时,我可以在 ie 上刷新我的 OneDrive 文件夹,但我永远看不到我要上传的内容。但是一旦我停止调试器,或者终止手机上的应用程序,我可以立即刷新,文件就会在那里。

上传方法如下:

public async Task UploadToOneDrive(string folderID, string localFileName)
    {
        try
        {
            StorageFile file = await ApplicationData.Current.LocalFolder.GetFileAsync(localFileName);
            string fileName = "backup-" + DateTime.Now.ToString("dd-MM") + ".db";
            await file.CopyAsync(ApplicationData.Current.LocalFolder, fileName, NameCollisionOption.ReplaceExisting);
            file = await ApplicationData.Current.LocalFolder.GetFileAsync(fileName);

            await connectClient.BackgroundUploadAsync(folderID,
                fileName, file, OverwriteOption.Overwrite);
        }
        catch (LiveConnectException)
        {
            MessageDialog m = new MessageDialog("Could not connect to to OneDrive. Cloud sync will be stopped.");
            m.ShowAsync();
        }
        catch (LiveAuthException)
        {
            MessageDialog m = new MessageDialog("Error authenticating OneDrive service. Please try cloud sync again later.");
            m.ShowAsync();
        }
        catch (Exception ex)
        {
            MessageDialog m = new MessageDialog("Unknown exception occurred.\n\nError:{0}", ex.Message);
            m.ShowAsync();
        }
    }

这是同步过程

public async Task sync()
    {
        var sb = StatusBar.GetForCurrentView();
        sb.ProgressIndicator.Text = "Syncing with OneDrive...";
        await sb.ProgressIndicator.ShowAsync();

        string cloudFolderID = await syncManager.CreateOrGetOneDriveFolderID("GlucoseCalculator", "Documents/");
        string cloudFileID = await syncManager.GetFileID(DataManager.sqlFileName, "Documents/GlucoseCalculator/");

        try
        {

            if (cloudFileID != null)
            {
                if (!(await dbManager.DoesFileExist(DataManager.sqlFileName)))
                {
                    sb.ProgressIndicator.Text = "Downloading file from OneDrive...";
                    await syncManager.DownloadFromOneDrive(cloudFileID, DataManager.sqlFileName);
                    goto BREAK;
                }

                DateTime cloudLastEditDateTime = DateTime.Parse(await syncManager.GetFileProperty(cloudFileID, "updated_time"));
                DateTime localLastEditDateTime = ApplicationData.Current.LocalFolder.GetFileAsync(DataManager.sqlFileName).GetResults().GetBasicPropertiesAsync().GetResults().DateModified.DateTime;

                if (cloudLastEditDateTime > localLastEditDateTime)
                {
                    sb.ProgressIndicator.Text = "Downloading file from OneDrive...";
                    await syncManager.DownloadFromOneDrive(cloudFileID, DataManager.sqlFileName);
                }

                else if (cloudLastEditDateTime < localLastEditDateTime)
                {
                    sb.ProgressIndicator.Text = "Uploading file to OneDrive...";
                    await syncManager.UploadToOneDrive(cloudFolderID, DataManager.sqlFileName);
                }

            }
            else if (cloudFileID == null)
            {
                sb.ProgressIndicator.Text = "Uploading new file to OneDrive...";                                
                await syncManager.UploadToOneDrive(cloudFolderID, DataManager.sqlFileName);
            }
        }
        catch (Exception)
        {
            sb.ProgressIndicator.Text = "Cloud synchronization failed.";
            sb.ProgressIndicator.HideAsync();
            return;
        }

        sb.ProgressIndicator.Text = "Synchronization complete!";
        BREAK:;
        await sb.ProgressIndicator.HideAsync();
    }

【问题讨论】:

  • 在梳理了大量的 Microsoft 帐户设置和东西后,我终于找到了如何撤销应用程序权限。我为我的应用程序做了这个,卸载了它,重新启动了我的电脑,它现在似乎可以工作了。仍然不知道是什么导致了问题。

标签: c# windows-phone-8.1 onedrive


【解决方案1】:

很可能,您正在创建一些实现IDisposible 的对象,但它不在using 块中。也许是StorageFile

【讨论】:

  • 我尝试从单击同步按钮到上传点的那一刻浏览我的所有代码,但似乎没有实现 IDisposable。我是编程新手,所以我假设对象必须具有 Dispose() 方法。但是我也尝试安全并将存储文件放入 using 中,它给出了一个错误,说它必须可以隐式转换为 IDisposable。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-27
  • 1970-01-01
相关资源
最近更新 更多