从预定代理保存图像时出现c# – System.UnauthorizedAccessException
作者:互联网
我正在构建一个WP8应用程序,它使用来自Internet的图像来更改锁定屏幕的背景.我按照预定代理和锁屏的教程,但我有一个问题.
当我尝试从计划代理下载新的背景图像时,我得到:
+ $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.因此,您可能需要在Dispatcher调用中放置NotifyComplete.
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
UpdateSyncPictureName(...);
NotifyComplete();
});
来源:Invalid Cross Exception on Schedule Agent when working on isolated storage
标签:c,windows-phone-8,bitmapimage,lockscreen,unauthorizedaccessexcepti 来源: https://codeday.me/bug/20190529/1180384.html