我正在开发一个用 C# 编写的 Windows Mobile 6.1 应用程序。我最近添加了拍照功能。该设备是Intermec CN50。如果我打开我的应用程序并直接转到拍照位,则此代码可以正常工作。但是,如果我浏览其他几个屏幕,然后打开照片位,它会失败并显示非常有用的消息“发生未知错误”。我可以看到异常中没有其他有用的信息。代码在这一行失败:
cameraResult = cameraDialog.ShowDialog();
这是堆栈跟踪:
at Microsoft.WindowsMobile.Forms.CameraCaptureDialog.LaunchCameraCaptureDialog(IntPtr ptrStruct)
at Microsoft.WindowsMobile.Forms.CameraCaptureDialog.ShowDialog()
at MicronetMobileUi.Controls.Camera.ShowDialog(Form owner, String& fileName)
at MicronetMobileUi.FieldService.JobImagesForm.LoadCameraScreen()
at MicronetMobileUi.FieldService.JobImagesForm.footerToolbar_ItemEntered(Object sender, EventArgs e)
at Resco.Controls.CommonControls.ToolbarControl.OnItemEntered()
at Resco.Controls.CommonControls.ToolbarControl.MouseClickUp(MouseEventArgs e)
at Resco.Controls.CommonControls.ToolbarControl.OnMouseUp(MouseEventArgs e)
at System.Windows.Forms.Control.WnProc(WM wm, Int32 wParam, Int32 lParam)
at System.Windows.Forms.ContainerControl.WnProc(WM wm, Int32 wParam, Int32 lParam)
at System.Windows.Forms.Control._InternalWnProc(WM wm, Int32 wParam, Int32 lParam)
at Microsoft.AGL.Forms.EVL.EnterMainLoop(IntPtr hwnMain)
at System.Windows.Forms.Application.Run(Form fm)
at MicronetMobileUi.ApplicationManager.StartApp()
at MicronetMobileUi.Program.Main()
这是代码:
var path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);
var fileName = "temp.jpg";
var pathAndFileName = Path.Combine(path, fileName);
var cameraDialog = new CameraCaptureDialog();
cameraDialog.Owner = this;
cameraDialog.InitialDirectory = path;
cameraDialog.DefaultFileName = fileName;
cameraDialog.Mode = CameraCaptureMode.Still;
cameraDialog.StillQuality = CameraCaptureStillQuality.Low;
cameraDialog.VideoTimeLimit = new TimeSpan(0, 0, 0);
cameraDialog.VideoTypes = CameraCaptureVideoTypes.Messaging;
cameraDialog.Resolution = new Size(640, 480);
// open camera dialog to take a photo
DialogResult cameraResult;
try
{
cameraResult = cameraDialog.ShowDialog();
}
catch (Exception ex)
{
MessageBox.Show("Grrrr!!");
}
finally
{
cameraDialog.Dispose();
cameraDialog = null;
}
if (cameraResult != DialogResult.OK)
return; // no photo was taken
// save photo using this value: pathAndFileName ...
我想这与内存不足有关,但我真的不知道。到目前为止,我已经尝试过:
1)在应用程序第一次启动时 Instatiating CameraCaptureDialog 以便它可以获得一些内存。
2) Instatiating CameraCaptureDialog 并在应用程序首次启动时调用 ShowDialog()。
3) 在实例化 CameraCaptureDialog 之前关闭我与本地 SQL CE 数据库的连接(这是在论坛上建议的)。
4)在实例化之前运行 GC.Collect() (是的,我知道这通常不是一个好主意)。
5) 将 cameraDialog.Resolution 减少到 320 x 240。
没有任何效果。我花了很长时间将这个功能添加到我的应用程序中,现在我发现它有时只真正起作用。Windows Mobile 可能非常烦人。帮助!!