WPF and Powershell: Using a custom compiled window type(Part 4)

It’s probably time that I post the third example of how to use Library-PresentationInterface. I am sorry about the delay. I had the code for all the examples compiled before even posting the first example. The only thing keeping me from posting the example scripts is that I have to write the commentary. So I have no valid excuse for the delay.

The purpose of this example is to show that Library-PresentationInterface can be used to load a custom compiled window class using only that class’s type name. The script includes a call to New-CAssembly to compile a new window class. A call to Start-PresentationInterface is then made with the new classes name passed to the WindowType parameter.

You may wish to save this code! I have found it very useful for debugging! The result of this script is a GUI representation of any exception or error record passed to the script! Also the Wait switch parameter may be used to halt script execution while the window is open, the dialog buttons then may be used to return a true/false value from the script witch may be used by any error trapping code.

Here it is, all 145 lines of it…

param ($ErrorInfo, [Switch]$Wait)

. Library-PresentationInterface

[void](new-cassembly @'
using System;
using System.Management.Automation;
using System.Windows;
using System.Windows.Controls;

namespace ErrorWindow {
	public class ErrorWindowClass : Window {
		bool IsErrorRecord;
		ErrorRecord Error;
		Exception Ex;
		protected override void OnInitialized(EventArgs e) {
			base.OnInitialized(e);
			this.Title = "ErrorWindow";
			if ((this.Tag is ErrorRecord) || (this.Tag is Exception)) {
				this.Height = 400;
				this.Width = 800;
				
				StackPanel Stack = new StackPanel();
				if (this.Tag is ErrorRecord) {
					IsErrorRecord = true;
					Error = (ErrorRecord)(this.Tag);
					Ex = Error.Exception;
				}
				else {
					IsErrorRecord = false;
					Ex = (Exception)(this.Tag);
				}
				
				Label MessageLabel = new Label();
				MessageLabel.Content = Ex.Message;
				Stack.Children.Add(MessageLabel);
				
				if (IsErrorRecord) {
					Label IdLabel = new Label();
					IdLabel.Content = String.Format("Error ID: {0}",Error.FullyQualifiedErrorId);
					Stack.Children.Add(IdLabel);

					Label CategoryLabel = new Label();
					CategoryLabel.Content = String.Format("Error Category: {0}",Error.CategoryInfo.Category.ToString());
					Stack.Children.Add(CategoryLabel);

					if (Error.CategoryInfo.Activity != "") {
						Label ActivityLabel = new Label();
						ActivityLabel.Content = String.Format("Error Activity: {0}",Error.CategoryInfo.Activity);
						Stack.Children.Add(ActivityLabel);
					}

					if (Error.CategoryInfo.Reason != "") {
						Label ReasonLabel = new Label();
						ReasonLabel.Content = String.Format("Error Reason: {0}",Error.CategoryInfo.Reason);
						Stack.Children.Add(ReasonLabel);
					}

					if (Error.CategoryInfo.TargetName != "") {
						Label TargetNameLabel = new Label();
						TargetNameLabel.Content = String.Format("Target Name: {0}",Error.CategoryInfo.TargetName);
						Stack.Children.Add(TargetNameLabel);
					}

					if (Error.CategoryInfo.TargetType != "") {
						Label TargetTypeLabel = new Label();
						TargetTypeLabel.Content = String.Format("Target Type: {0}",Error.CategoryInfo.TargetType);
						Stack.Children.Add(TargetTypeLabel);
					}

					Label PositionLabel = new Label();
					PositionLabel.Content = String.Format("Error Position: {0}",Error.InvocationInfo.PositionMessage);
					Stack.Children.Add(PositionLabel);
				}
				
				Label TypeLabel = new Label();
				TypeLabel.Content = String.Format("Error Type: {0}",Ex.GetType().FullName);
				Stack.Children.Add(TypeLabel);
				
				if (Ex.InnerException != null) {
					StackPanel InnerStack = new StackPanel();
					InnerStack.Orientation = Orientation.Horizontal;
					Label InnerExLabel = new Label();
					InnerExLabel.Content = String.Format("Inner Exception: {0}",Ex.InnerException.Message);
					InnerStack.Children.Add(InnerExLabel);
					
					Button InnerExButton = new Button();
					InnerExButton.Content = "More Info";
					InnerExButton.Click += this.InnerExceptionInfoClick;
					InnerStack.Children.Add(InnerExButton);
					Stack.Children.Add(InnerStack);
				}
				
				TextBlock StackBlock = new TextBlock();
				StackBlock.Text = String.Format("Stack:n{0}",Ex.StackTrace);
				Stack.Children.Add(StackBlock);
				
				if (this.Owner == null) {
					StackPanel DialogStack = new StackPanel();
					DialogStack.Orientation = Orientation.Horizontal;
					DialogStack.HorizontalAlignment = HorizontalAlignment.Center;
					Button ContinueButton = new Button();
					ContinueButton.Content = "Continue/Retry";
					ContinueButton.Click += this.ContinueClick;
					DialogStack.Children.Add(ContinueButton);
					
					Button BreakButton = new Button();
					BreakButton.Content = "Break/Fail";
					BreakButton.Click += this.BreakClick;
					DialogStack.Children.Add(BreakButton);
					Stack.Children.Add(DialogStack);
				}
				
				ScrollViewer ScrollView = new ScrollViewer();
				ScrollView.Content = Stack;
				ScrollView.HorizontalScrollBarVisibility = ScrollBarVisibility.Auto;
				this.Content = ScrollView;
			}
			else {
				this.Height = 100;
				this.Width = 500;
				this.Content="Target object is not an ErrorRecord.";
			}
		}

		public void InnerExceptionInfoClick(object sender, RoutedEventArgs e) {
			ErrorWindowClass ChildWindow = new ErrorWindowClass();
			ChildWindow.Tag = Ex.InnerException;
			ChildWindow.Owner = this;
			ChildWindow.Show();
		}

		public void ContinueClick(object sender, RoutedEventArgs e) {
			this.DialogResult = true;
		}

		public void BreakClick(object sender, RoutedEventArgs e) {
			this.DialogResult = false;
		}
	}
}
'@)

$result = start-PresentationInterface -WindowType ([ErrorWindow.ErrorWindowClass]) -WindowTag $ErrorInfo
if ($Wait) {(stop-PresentationInterface $result).DialogResult}

I suggest saving it as Get-ErrorDialog.

To use it simply perform any action that would cause an exception. Then, you may read the exception straight from the $error variable.

Get-ErrorDialog $Error[0]
Advertisements

~ by lunaticexperimentalist on January 30, 2008.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: