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


            }
        }
    }
4

1 回答 1

5

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

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

Deployment.Current.Dispatcher.BeginInvoke(() =>
{
    UpdateSyncPictureName(...);
    NotifyComplete();
});
于 2012-12-10T22:17:28.687 回答