C# 4.0 在独立存储上工作时,Schedule Agent上的交叉异常无效
我正在使用Windows Phone Schedule Agent,并尝试在同步后更新图片名称。问题是,当我在此函数的“BitmapImage bmp=new BitmapImage();”行上遇到无效的交叉异常,我真的不明白为什么C# 4.0 在独立存储上工作时,Schedule Agent上的交叉异常无效,c#-4.0,windows-phone-7.1,isolatedstorage,C# 4.0,Windows Phone 7.1,Isolatedstorage,我正在使用Windows Phone Schedule Agent,并尝试在同步后更新图片名称。问题是,当我在此函数的“BitmapImage bmp=new BitmapImage();”行上遇到无效的交叉异常,我真的不明白为什么 void UpdateSyncPictureName(int AsyncStatus, int AticketID, int AsyncID, int ApictureID, int TsyncStatus = 0, int TsyncID = 0) {
void UpdateSyncPictureName(int AsyncStatus, int AticketID, int AsyncID, int ApictureID, int TsyncStatus = 0, int TsyncID = 0)
{
string filename = AsyncStatus + "-" + AticketID + "-" + AsyncID + "-" + ApictureID;
using (IsolatedStorageFile ISF = IsolatedStorageFile.GetUserStoreForApplication())
{
if (ISF.FileExists(filename))
{
BitmapImage bmp = new BitmapImage();
using (IsolatedStorageFileStream isoStream =
ISF.OpenFile(filename, System.IO.FileMode.Open))
{
bmp.SetSource(isoStream);
}
ISF.DeleteFile(filename);
WriteableBitmap Wbmp = new WriteableBitmap(bmp);
using (IsolatedStorageFileStream isoStream =
ISF.OpenFile(TsyncStatus + "-" + AticketID + "-" + TsyncID + "-" + ApictureID, System.IO.FileMode.Create))
{
Extensions.SaveJpeg(Wbmp, isoStream,
Wbmp.PixelWidth,
Wbmp.PixelHeight,
0, 100);
}
}
}
}
该问题源于无法在UI线程之外实例化BitmapImage这一事实。您可以通过在Dispatcher Invoke调用中包装调用来修复此问题 但是,您需要确保正确调用NotifyComplete。因此,您可能需要在Dispatcher调用中输入NotifyComplete
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
UpdateSyncPictureName(...);
NotifyComplete();
});
该问题源于无法在UI线程之外实例化BitmapImage这一事实。您可以通过在Dispatcher Invoke调用中包装调用来修复此问题 但是,您需要确保正确调用NotifyComplete。因此,您可能需要在Dispatcher调用中输入NotifyComplete
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
UpdateSyncPictureName(...);
NotifyComplete();
});