处理隔离存储时,Schedule Agent上的交叉异常无效

Chr*_*uez 1 isolatedstorage c#-4.0 windows-phone-7.1

我正在使用Windows Phone Schedule Agent,我试图在同步后尝试更新图片名称问题是我在"BitmapImage bmp = new BitmapImage();"这一行的此功能上遇到无效的交叉异常 而且真的不明白为什么.

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);
                }


            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

twi*_*hax 5

问题产生于BitmapImage无法在UI线程之外实例化.您可以通过在Dispatcher Invoke调用中包装调用来解决此问题.

但是,您需要确保正确调用NotifyComplete.因此,您可能需要在Dispatcher调用中放置NotifyComplete.

Deployment.Current.Dispatcher.BeginInvoke(() =>
{
    UpdateSyncPictureName(...);
    NotifyComplete();
});
Run Code Online (Sandbox Code Playgroud)