Sql Server ExceptionMessageBox in WPF nutzen

Wer das Microsoft Sql Management Studio verwendet kennt sie vielleicht, die ExceptionMessageBox des Sql Servers. Sie bietet eine gute Übersicht über alle relevanten Informationen einer Exception und das Beste ist, dass sie als Assembly in jedes .NET Projekt eingebunden und damit in jeder .NET Anwendung angezeigt werden kann.

image

Einziger Haken: Zur Anzeige der ExceptionMessageBox wird ein owner vom Typ System.Windows.Forms.IWin32Owner erwartet. Bewegt man sich in WPF, steht dieser einem natürlich nicht zur Verfügung. Abhilfe schafft der Typ ExceptionMessageBoxParent, da dieser mit einem gewöhnlichen Handle erstellt werden und somit als IWin32Owner verwendet werden kann.

Mit den WPF Interop Tools kann somit die ExceptionMessageBox problemlos in WPF einbinden.

   1:      public class ExceptionMessageBoxWpf : ExceptionMessageBox
   2:      {
   3:          public ExceptionMessageBoxWpf() : base() { }
   4:          public ExceptionMessageBoxWpf(Exception exception) : base(exception) { }
   5:          public ExceptionMessageBoxWpf(string text) : base(text) { }
   6:          public ExceptionMessageBoxWpf(Exception exception, ExceptionMessageBoxButtons buttons) : base(exception, buttons) { }
   7:          public ExceptionMessageBoxWpf(string text, string caption) : base(text, caption) { }
   8:          public ExceptionMessageBoxWpf(Exception exception, ExceptionMessageBoxButtons buttons, ExceptionMessageBoxSymbol symbol) : base(exception, buttons, symbol) { }
   9:          public ExceptionMessageBoxWpf(string text, string caption, ExceptionMessageBoxButtons buttons) : base(text, caption, buttons) { }
  10:          public ExceptionMessageBoxWpf(Exception exception, ExceptionMessageBoxButtons buttons, ExceptionMessageBoxSymbol symbol, ExceptionMessageBoxDefaultButton defaultButton) : base(exception, buttons, symbol, defaultButton) { }
  11:          public ExceptionMessageBoxWpf(string text, string caption, ExceptionMessageBoxButtons buttons, ExceptionMessageBoxSymbol symbol) : base(text, caption, buttons, symbol) { }
  12:          public ExceptionMessageBoxWpf(Exception exception, ExceptionMessageBoxButtons buttons, ExceptionMessageBoxSymbol symbol, ExceptionMessageBoxDefaultButton defaultButton, ExceptionMessageBoxOptions options) : base(exception, buttons, symbol, defaultButton, options) { }
  13:          public ExceptionMessageBoxWpf(string text, string caption, ExceptionMessageBoxButtons buttons, ExceptionMessageBoxSymbol symbol, ExceptionMessageBoxDefaultButton defaultButton) : base(text, caption, buttons, symbol, defaultButton) { }
  14:          public ExceptionMessageBoxWpf(string text, string caption, ExceptionMessageBoxButtons buttons, ExceptionMessageBoxSymbol symbol, ExceptionMessageBoxDefaultButton defaultButton, ExceptionMessageBoxOptions options) : base(text, caption, buttons, symbol, defaultButton, options) { }
  15:   
  16:          public void Show(Window owner) {
  17:              // the owner window is required
  18:              if (owner == null) throw new ArgumentNullException("owner");
  19:   
  20:              Type exbParentType;
  21:              Type exbType;
  22:              System.Windows.Interop.WindowInteropHelper helper;
  23:   
  24:              // The ExceptionMessageBox Show method requires a owner which implements
  25:              // System.Windows.Forms.IWin32Window - the WPF Windows don't do that
  26:              // but we can use the internal class ExceptionMessageBoxParent
  27:              // the create a appropriate owner.
  28:              exbParentType = Type.GetType("Microsoft.SqlServer.MessageBox.ExceptionMessageBoxParent, Microsoft.ExceptionMessageBox, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91");
  29:              exbType = this.GetType();
  30:   
  31:              // WindowInteropHelper allows access to the root handle of the wpf window.
  32:              helper = new System.Windows.Interop.WindowInteropHelper(owner);
  33:   
  34:              IWin32Window parent = (IWin32Window)Activator.CreateInstance(exbParentType, new object[] { helper.Handle });
  35:   
  36:              this.Show(parent);
  37:          }
  38:  }

Als Parameter lässt sich somit jedes WPF Window übergeben.

   1:              try {
   2:                  throw new ApplicationException("Hello from WPF!");
   3:              } catch (Exception ex) {
   4:                  ExceptionMessageBoxWpf ebx = new ExceptionMessageBoxWpf(ex);
   5:                  ebx.Show(this);
   6:                  throw;
   7:              }
.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }

Die ExceptionMessageBox ist jedoch erstaunlich vielseitig und lässt sich auch für Meldungen jeglicher Art verwenden (siehe [2]).

Zum Weiterlesen

[1] How to: Program Exception Message Box (MSDN)
http://msdn.microsoft.com/en-us/library/ms166340.aspx

[2] Make your exceptions shine with SQL Server Exception Message Box
http://geekswithblogs.net/kobush/archive/2006/05/21/ExceptionMessageBox.aspx

Published Sonntag, 22. März 2009 13:27 von Jan-Cornelius Molnar
Abgelegt unter: , ,

Kommentare

# Sql Server ExceptionMessageBox in WPF nutzen

Sonntag, 22. März 2009 15:42 von dotnet-kicks.de

Sie wurden gekickt (eine gute Sache) - Trackback von dotnet-kicks.de