Exception viewing with TaskDialog

Jun 16, 2010 at 6:00 PM

 

I added this static routine to display exceptions using the TaskDialog.  I thought i'd post in hopes that someone might find it useful.

Michael

 

        public static void ShowException(Exception ex)
        {
            TaskDialog dlg = new TaskDialog
            {
                MaxWidth = 500,
                MainIcon = TaskDialogIconConverter.ConvertFrom(TaskDialogIcon.Error),
                FooterIcon = TaskDialogIconConverter.ConvertFrom(TaskDialogIcon.Information),
                Title = String.Format("{0} Exception", ex.GetType().Name)
            };

            StackPanel panel = new StackPanel();
            panel.Children.Add(new TextBlock()
            {
                Text = ex.Message,
                TextWrapping = TextWrapping.Wrap,
                FontWeight = FontWeights.Bold
            });
            Exception iex = ex.InnerException;
            while (iex != null)
            {
                panel.Children.Add(new TextBlock()
                {
                    Text = iex.Message,
                    TextWrapping = TextWrapping.Wrap
                });
                iex = iex.InnerException;
            }
            dlg.Content = panel;
            dlg.ExpansionContent = ex.StackTrace;
            dlg.ExpansionButtonContent = "Stack Trace";
            dlg.ExpansionPosition = TaskDialogExpansionPosition.Footer;

            TaskDialogButtons buttons = new TaskDialogButtons();
            buttons |= TaskDialogButtons.Close;
            foreach (object item in TaskDialogButtonData.FromStandardButtons(buttons))
                dlg.Buttons.Add(item);

            dlg.Show();
        }