2012-10-15 129 views
1

我正在使用Windows Phone調度代理,我試圖在同步後更新圖片名稱問題是我在此函數時遇到無效的異常行「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); 
       } 


      } 
     } 
    } 

回答

5

問題出在BitmapImage不能在UI線程之外實例化的事實。您可以通過將調用包裝在Dispatcher Invoke調用中來解決此問題。

但是,您需要確保您正確調用NotifyComplete。因此,您可能需要將NotifyComplete置於分派器調用中。

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