Monday, June 18, 2007

WPF Interop with ExceptionMessageBox

Update: Apparently there is a better way to do it, as explained at IEnumerable<Stuff>
I've been porting part of a Windows Forms project to Windows Presentation Foundation. I've been using ExceptionMessageBox to show error messages. Its Show method takes an IWin32Window argument. However, WPF forms does not implement the IWin32Window, so there is no way to directly pass System.Windows.Window into the argument. To 'convert' a WPF Window into IWin32Window, I've used the following

try

{

throw new Exception();

}

catch (Exception ex)

{

ExceptionMessageBox msgBox = new ExceptionMessageBox(

ex,

ExceptionMessageBoxButtons.OK,

ExceptionMessageBoxSymbol.Error,

ExceptionMessageBoxDefaultButton.Button1);

System.Windows.Interop.WindowInteropHelper windowInterop = new System.Windows.Interop.WindowInteropHelper(this);

System.Windows.Forms.Control winForm = System.Windows.Forms.Form.FromHandle(windowInterop.Handle);

msgBox.Show(winForm);

}

For those who have not seen the gem called ExceptionMessageBox, visit http://geekswithblogs.net/cicorias/archive/2006/05/09/77715.aspx and http://geekswithblogs.net/kobush/archive/2006/05/21/ExceptionMessageBox.aspx

No comments: