【问题标题】:Tracking changes of the pictures library on Windows 10 Mobile devices跟踪 Windows 10 移动设备上图片库的变化
【发布时间】:2018-06-22 06:22:11
【问题描述】:

我正在尝试跟踪图片库的更改,因为我希望我的应用程序将新照片上传到服务器。为了跟踪更改,我在这里关注了 MSDN 文章 https://msdn.microsoft.com/en-us/magazine/mt790201.aspx

如果我在手机上运行我的代码(带有 Fall Creators Update 的 Windows 10 移动版),如果将图片保存到 sd 卡,它将无法工作。但是,如果删除 sd 卡并重新启动我的手机,我可以从更改跟踪器中读取更改。在台式电脑上一切正常。

这就是我为更改跟踪器启用后台任务的方式:

    public async Task Register()
    {
        // Check if your app has access to the background
        var requestStatus = await BackgroundExecutionManager.RequestAccessAsync();
        if (!(requestStatus ==
              BackgroundAccessStatus.AllowedMayUseActiveRealTimeConnectivity ||
              requestStatus == BackgroundAccessStatus.AllowedSubjectToSystemPolicy ||
              requestStatus ==
              BackgroundAccessStatus.AllowedWithAlwaysOnRealTimeConnectivity ||
              requestStatus == BackgroundAccessStatus.AlwaysAllowed))
        {
            Debug.WriteLine("Failed to get access to the background");
            return;
        }
        // Build up the trigger to fire when something changes in the pictures library
        var builder = new BackgroundTaskBuilder();
        builder.Name = "Photo Change Trigger";
        StorageLibrary picturesLib =
            await StorageLibrary.GetLibraryAsync(KnownLibraryId.Pictures);
        var picturesTrigger = StorageLibraryContentChangedTrigger.Create(picturesLib);
        // We are registering to be activated in OnBackgroundActivated instead of
        // BackgroundTask.Run; either works, but I prefer the single-process model
        builder.SetTrigger(picturesTrigger);
        BackgroundTaskRegistration task = builder.Register();
    }

这就是我获得更改的方式,其中包含不起作用的代码:

    public async Task GetChanges()
    {
            StorageLibrary picturesLib =
                await StorageLibrary.GetLibraryAsync(KnownLibraryId.Pictures);
            StorageLibraryChangeTracker picturesTracker = picturesLib.ChangeTracker;
            picturesTracker.Enable();
            StorageLibraryChangeReader changeReader = picturesTracker.GetChangeReader();
            // if photos are saved on the SD-card the next line does not work
            IReadOnlyList<StorageLibraryChange> changes = await changeReader.ReadBatchAsync();
    }

是我做错了什么还是这是 Windows 10 移动版中的错误?我已经尝试将我的设备恢复出厂设置或重新格式化我的 SD 卡,但没有任何效果。

【问题讨论】:

  • 在手机上运行代码并将图片保存到SD卡时,能否告诉我们是否遇到任何错误或返回null?根据这篇文章:msdn.microsoft.com/en-us/magazine/mt790201.aspx,它表明由于 SD 卡可以在设备关闭时从设备中移除,并且基于 FAT 的文件系统上没有日志,因此无法保证在引导期间在 SD 卡上进行更改跟踪会议。
  • 我得到一个异常:HRESULT 异常:0x80080222 是的,图片已保存到 SD 卡,设备一直处于开机状态,无需重启。

标签: c# uwp windows-10-universal windows-10-mobile change-tracking


【解决方案1】:

official blogpost中所述:

调用 Enable()

上面提到过,但只是为了确保清楚:应用程序应在开始跟踪文件系统时以及在每次枚举更改之前调用 ChangeTracker.Enable()。这将确保更改跟踪器不会错过对库中包含的文件夹的更改。

所以我建议在任务注册后立即在Register 方法中调用Enable

【讨论】:

  • 你是对的,我应该在注册任务后立即调用它。我添加了这个,但不幸的是它并没有解决问题。
  • 哦,太不幸了...我也会在我的设备上尝试一下,看看它是否对我有用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-17
  • 1970-01-01
  • 2013-04-10
  • 1970-01-01
相关资源
最近更新 更多