【发布时间】:2011-10-25 14:40:29
【问题描述】:
如果一个文件被另一个应用程序打开,然后我尝试通过 Silverlight SaveDialog 保存它,我可以捕捉到异常错误,但之后我得到了这个错误。
行:57
错误:Silverlight 应用程序中未处理的错误
代码:4004
类别:ManagedRuntimeError
消息:System.InvalidOperationException:此操作只能在 UI 线程上发生。
在 System.Windows.Hosting.NativeHost.VerifyThread()
在 System.Windows.SaveFileStream.Dispose(布尔处理)
在 System.IO.FileStream.Finalize()
我更愿意检测文件是否打开,但似乎无法做到这一点。我试过 fs.CanWrite,但它返回 true,即使文件被另一个应用程序打开。
编辑:Here 是 silverlight 论坛上的一个帖子,似乎解释了正在发生的事情,尽管他们认为这只是 Office 文件。我遇到了 PDF 文件的问题。
这是我的代码:
public void PDFSaveFile(bool success)
{
// silverlight requires saveFileDialog to be user-initiated,
// so this is called from the OK button of a pop-up window
// ignore success, we only gave an OK option
byte[] fileBytes = doc.ToPDF().ToArray();
PDFClose();
try
{
SaveFileDialog saveFileDlg = new SaveFileDialog();
saveFileDlg.Filter = "PDF files (*.pdf)|*.pdf";
bool? dialogResult = saveFileDlg.ShowDialog();
if (dialogResult == true)
{
using (var fs = saveFileDlg.OpenFile())
{
fs.Write(fileBytes, 0, fileBytes.Length);
fs.Close();
}
}
}
catch (Exception ex)
{
Log.HandleInternalError(string.Format("Unable to save file: {0}",ex.Message));
}
}
【问题讨论】:
标签: c# silverlight savefiledialog