c# - 从 Scheduled Agent 保存图像时出现 System.UnauthorizedAccessException

标签 c# windows-phone-8 bitmapimage lockscreen unauthorizedaccessexcepti

我正在构建一个 WP8 应用程序,它使用来自 Internet 的图像更改锁屏界面的背景。我遵循了 Scheduled Agents 和 Lockscreen 上的教程,但我遇到了问题。

当我尝试从 Scheduled Agent 下载新的背景图片时,我得到了这个:

+       $exception  {System.UnauthorizedAccessException: Invalid cross-thread access.
   at MS.Internal.XcpImports.CheckThread()
   at System.Windows.DependencyObject..ctor(UInt32 nativeTypeIndex, IntPtr constructDO)
   at System.Windows.Media.Imaging.BitmapImage..ctor()
   at TileLockAgent.ScheduledAgent.lockScreenClient_OpenReadCompleted(Object sender, OpenReadCompletedEventArgs e)
   at System.Net.WebClient.OnOpenReadCompleted(OpenReadCompletedEventArgs e)
   at System.Net.WebClient.OpenReadOperationCompleted(Object arg)
   at System.Threading.WaitCallback.Invoke(Object state)
   at System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
   at System.Threading.ThreadPoolWorkQueue.Dispatch()
   at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()}   System.Exception {System.UnauthorizedAccessException}

代码是:

string fileName;

try
{
    var currentImage = LockScreen.GetImageUri();

    if (currentImage.ToString().EndsWith("_1.jpg"))
    {
        fileName = "LockBackground_2.jpg";
    }
    else
    {
        fileName = "LockBackground_1.jpg";
    }
}
catch
{
    // lockscreen not set or prev owned by other app          
    fileName = "LiveLockBackground_1.jpg";
}

using (var isoStore = IsolatedStorageFile.GetUserStoreForApplication())
{
    var bi = new BitmapImage();
    bi.SetSource(e.Result);
    var wb = new WriteableBitmap(bi);
    using (var isoFileStream = isoStore.CreateFile(fileName))
    {
        var width = wb.PixelWidth;
        var height = wb.PixelHeight;
        Extensions.SaveJpeg(wb, isoFileStream, width, height, 0, 100);
    }
}

我真的不知道怎么解决这个问题。如果 BitmapImage 不工作,如何在预定代理中保存图像?我正在进行“跨线程访问”是什么意思?这些图像仅由预定代理创建和使用,因此任何人都不应访问它们。

最佳答案

这个问题是因为 BitmapImage 不能在 UI 线程之外被实例化。您可以通过将调用包装在 Dispatcher Invoke 调用中来解决此问题。

但是,您需要确保正确调用 NotifyComplete。因此,您可能需要将 NotifyComplete 放入 Dispatcher 调用中。

  Deployment.Current.Dispatcher.BeginInvoke(() =>
    {
        UpdateSyncPictureName(...);
        NotifyComplete();
    });

来源:Invalid Cross Exception on Schedule Agent when working on isolated storage

关于c# - 从 Scheduled Agent 保存图像时出现 System.UnauthorizedAccessException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13878986/

相关文章:

javascript - 防止 Windows Phone 8 应用程序中的弹跳效果

javascript - 如何在winjs中的repeater元素中绑定(bind)点击事件?

c# - 使用 MVVM,如何将 XamDataGrid 的 SelectedItems 作为参数传递给 ContextMenu 引发的命令?

c# - .NET C# socket 在两个不同的线程上发送

javascript - 单击网页中的复选框?

WPF - 旋转和保存图像 - 用于 View 和磁盘

c# - 如何在 WPF 中将位图渲染到 Canvas 中?

c# - 在 NHibernate 中动态更改查询类型

windows-phone-8 - "Can' t 在 Windows Phone 8 上安装公司应用程序

c# - 从 C# 在 TableCell 中插入图像