@@ -30,19 +30,16 @@ public IObservable<bool> GetActivationForView(IActivatableView view)
3030 {
3131 switch ( view )
3232 {
33- // Startup: Control.HandleCreated > Control.BindingContextChanged > Form.Load > Control.VisibleChanged > Form.Activated > Form.Shown
34- // Shutdown: Form.Closing > Form.FormClosing > Form.Closed > Form.FormClosed > Form.Deactivate
35- // https://docs.microsoft.com/en-us/dotnet/framework/winforms/order-of-events-in-windows-forms
36- case Control control when GetCachedIsDesignMode ( control ) :
37- break ;
3833 case Control control :
39- return GetActivationForControl ( control ) ;
34+ // We are very likely being called from control's constructor, which means control.Site is not yet set.
35+ // We must delay the design mode check until after one of the activation events fires.
36+ return new WhereObservable < bool > ( GetActivationForControl ( control ) , _ => ! GetCachedIsDesignMode ( control ) ) ;
4037
4138 case null :
4239 {
4340 this . Log ( ) . Warn (
44- CultureInfo . InvariantCulture ,
45- "Expected a view of type System.Windows.Forms.Control it was null" ) ;
41+ CultureInfo . InvariantCulture ,
42+ "Expected a view of type System.Windows.Forms.Control it was null" ) ;
4643 break ;
4744 }
4845
@@ -65,6 +62,9 @@ public IObservable<bool> GetActivationForView(IActivatableView view)
6562 /// <returns>An observable that signals when the control is activated and deactivated.</returns>
6663 private static MergedDistinctObservable < bool > GetActivationForControl ( Control control )
6764 {
65+ // Startup: Control.HandleCreated > Control.BindingContextChanged > Form.Load > Control.VisibleChanged > Form.Activated > Form.Shown
66+ // Shutdown: Form.Closing > Form.FormClosing > Form.Closed > Form.FormClosed > Form.Deactivate
67+ // https://docs.microsoft.com/en-us/dotnet/framework/winforms/order-of-events-in-windows-forms
6868 var handleDestroyed = new FromEventObservable < bool > ( onNext =>
6969 {
7070 void Handler ( object ? sender , EventArgs e ) => onNext ( false ) ;
0 commit comments