canton7 / Stylet

A very lightweight but powerful ViewModel-First MVVM framework for WPF for .NET Framework and .NET Core, inspired by Caliburn.Micro.
MIT License
981 stars 143 forks source link

OnInitialActivate does not get called in "Child" Screen #275

Closed uyaem closed 2 years ago

uyaem commented 2 years ago

Description OnInitialActivate does not get called in a "Child" Screen in controls if using <ContentControl> to embed the child. I cannot use any of the Conductor options on the parent, because there are potentially several "dynamic controls" on the page, as the application continues to grow. Is this a bug, or by design, or am I doing something wrong?

To Reproduce Below code snippet should suffice to reproduce the behaviour. This is an extremely stripped down version of my scenario, and I hope it is complete enough. If not, I'll amend as needed.

/// ParentView.xaml
[...]
    <ContentControl s:View.Model="{Binding DynamicControl}" [...] />
[...]
/// ParentViewModel.cs
public class ParentViewModel : Screen
{
  private Func<ChildViewModel> createChild;
  private Screen dynamicControl;

  public ParentViewModel(Func<ChildViewModel> createChild) { this.createChild = createChild; }

  public Screen DynamicControl
  {
    get => this.dynamicControl;
    set => this.SetAndNotify(ref this.dynamicControl, value);
  }

  protected override void OnInitialActivate()
  {
    base.OnInitialActivate();
    this.DynamicControl = this.createChild();
  }
}

/// ChildViewModel.cs
public class ChildViewModel : Screen
{
  protected override void OnInitialActivate()
  {
    // Never gets called
  }
}

Version Info