User Tools

Site Tools


exception_handler_wpf

Exception handler for a WPF application

The following piece of code can be used to display an exception text in a WPF GUI application. Please don't forget to log the exception also to a logfile. You can take this code also as sample how to build a WPF window from pure code.

class ErrorDisplay inherit Window
constructor( oEx as Exception ) 
	
self:Initialize( oEx )

return
	
method Initialize( oEx as Exception ) as void
local oGrid as Grid
local oTextBlock as TextBlock
local oButton as Button     
local oMargin as Thickness

oMargin := Thickness{ 10 }
// Grid	
oGrid := Grid{} 
oGrid:ColumnDefinitions:Add( ColumnDefinition{} )
oGrid:RowDefinitions:Add( RowDefinition{}{ Height := GridLength{ 200 } } )
oGrid:RowDefinitions:Add( RowDefinition{} )
oGrid:RowDefinitions:Add( RowDefinition{}{ Height := GridLength{ 100 } } ) 
// display the error
oTextBlock := TextBlock{}
oTextBlock:Text := oEx:Message
oTextBlock:Margin := oMargin
Grid.SetColumn( oTextBlock, 0 )
Grid.SetRow( oTextBlock, 0 )      
oGrid:Children:Add( oTextBlock )
// display the callstack
oTextBlock := TextBlock{}
oTextBlock:Text := oEx:StackTrace
oTextBlock:Margin := oMargin
Grid.SetColumn( oTextBlock, 0 )
Grid.SetRow( oTextBlock, 1 )      
oGrid:Children:Add( oTextBlock )
// Close button
oButton := Button{}
oButton:Content := "OK"
oButton:Click += OkButtonClick
oButton:Margin := oMargin
Grid.SetColumn( oButton, 0 )
Grid.SetRow( oButton, 2 )      
oGrid:Children:Add( oButton )
	
self:Content		:= oGrid
	
return
	
method OkButtonClick( oSender as object, e as RoutedEventArgs ) as void
	
self:Close()
	
return

end class
exception_handler_wpf.txt · Last modified: 2018/03/25 18:02 by wolfgangriedmann