77
88namespace ExceptionVisualizer
99{
10- internal class ExceptionUserControl : RemoteUserControl
10+ internal class ExceptionUserControl ( VisualizerTarget visualizerTarget ) : RemoteUserControl ( new ViewModel ( ) )
1111 {
12- private readonly VisualizerTarget visualizerTarget ;
12+ private readonly VisualizerTarget visualizerTarget = visualizerTarget ;
1313
14- public ExceptionUserControl ( VisualizerTarget visualizerTarget ) : base ( new ViewModel ( ) )
15- {
16- this . visualizerTarget = visualizerTarget ;
17- }
18-
19- private ViewModel ViewModel => ( ViewModel ) this . DataContext ! ;
14+ private ViewModel ViewModel => ( ViewModel ) DataContext ! ;
2015
2116
2217 public override Task ControlLoadedAsync ( CancellationToken cancellationToken )
@@ -28,7 +23,7 @@ public override Task ControlLoadedAsync(CancellationToken cancellationToken)
2823 ExceptionModel ? exception = await this . visualizerTarget . ObjectSource . RequestDataAsync < ExceptionModel ? > ( jsonSerializer : null , CancellationToken . None ) ;
2924 if ( exception != null )
3025 {
31- var viewModel = ToViewModel ( exception ) ;
26+ var viewModel = exception . ToViewModel ( ) ;
3227 ViewModel . Exceptions . Add ( viewModel ) ;
3328 Subscribe ( viewModel ) ;
3429 viewModel . IsSelected = true ;
@@ -43,7 +38,7 @@ public override Task ControlLoadedAsync(CancellationToken cancellationToken)
4338 MessageBox . Show ( $ "ExceptionVisualizer failed with exception:\n { ex } ") ;
4439 Telemetry . TrackException ( ex ) ;
4540 }
46- } ) ;
41+ } , cancellationToken ) ;
4742 return Task . CompletedTask ;
4843 }
4944
@@ -56,43 +51,9 @@ private void Subscribe(ExceptionViewModel model)
5651 }
5752 }
5853
59- private ExceptionViewModel ToViewModel ( ExceptionModel exception )
60- {
61- var viewModel = new ExceptionViewModel
62- {
63- Data = new ObservableCollection < DataViewModel > ( exception . Data . Select ( d => new DataViewModel
64- {
65- Key = d . Key ,
66- Value = d . Value ,
67- } ) ) ,
68- Properties = new ObservableCollection < DataViewModel > ( exception . Properties . Select ( d => new DataViewModel
69- {
70- Key = d . Key ,
71- Value = d . Value ,
72- } ) ) ,
73- ShowData = exception . Data . Count > 0 ? Visibility . Visible : Visibility . Collapsed ,
74- ShowProperties = exception . Properties . Count > 0 ? Visibility . Visible : Visibility . Collapsed ,
75- HelpLink = exception . HelpLink ,
76- HResult = exception . HResult ,
77- Message = exception . Message ,
78- Source = exception . Source ,
79- StackTrace = exception . StackTrace ,
80- TargetSite = exception . TargetSite ,
81- Demystified = exception . Demystified ,
82- @Type = exception . Type ,
83- } ;
84- foreach ( var inner in exception . InnerExceptions )
85- {
86- viewModel . InnerExceptions . Add ( ToViewModel ( inner ) ) ;
87- }
88-
89- return viewModel ;
90- }
91-
9254 private void Exception_PropertyChanged ( object ? sender , System . ComponentModel . PropertyChangedEventArgs e )
9355 {
94- var model = sender as ExceptionViewModel ;
95- if ( model == null ) return ;
56+ if ( sender is not ExceptionViewModel model ) return ;
9657
9758 if ( model . IsSelected )
9859 {
0 commit comments