title: Call method on render page content from parent window WPF
body: <p>I'm developing a page-based program so I have a main menu that loads a Basewindow that contains a frame with the page inside depending the specified uri as shown:</p>
  
  <p>BaseWindow xaml code:</p>
  
  <pre><code>&lt;Window ...&gt;
      &lt;Toolbar&gt;
          &lt;Button&gt;
          &lt;/Button&gt;
          &lt;!--
              Same toolbar for all pages
          --&gt;
      &lt;/Toolbar&gt;
  
      &lt;StackPanel Name="c"&gt;
          &lt;Border BorderThickness="0,2,0,0"&gt;
              &lt;Border.BorderBrush&gt;
                  &lt;SolidColorBrush Color="{DynamicResource {x:Static SystemColors.ActiveCaptionColorKey}}"/&gt;
              &lt;/Border.BorderBrush&gt;
          &lt;/Border&gt;
  
          &lt;Frame Name="contentFrame" NavigationUIVisibility="Hidden" /&gt;
      &lt;/StackPanel&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>BaseWindow C#:</p>
  
  <pre><code>public Uri FormUri {
      get =&gt; formUri;
      set {
          if (value != null) {
              formUri = value;
              //sets the content inside frame
              MainFrame.Navigate(formUri);
          }
      }
  }
  
  public BaseWindow(Uri formUri) {
      InitializeComponent();
  
      FormUri = formUri;
  
      MainFrame.ContentRendered += MainFrame_ContentRendered;
  }
  
  private void MainFrame_ContentRendered(object sender, EventArgs e) {
      content = (IBasePage)MainFrame.Content;
  }
  
  public void FirstRegister() {
      content.FirstRegister();
  }
  
  /*
      some methods
  */
  </code></pre>
  
  <p>The problem is that I need to call <code>FirstRegister()</code> from outside this class and if the window is closed / content not loaded it throws an exception so what I suppose I need is to take the <code>MainFrame.ContentRendered</code> event and make it public and in the classes I need to call the content method do it inside the event signature.</p>
  
  <p>This is the exception:</p>
  
  <pre><code>System.NullReferenceException: 'Object reference not set to an instance of an object.'
  </code></pre>
  
  <p>in this line: </p>
  
  <pre><code>public void FirstRegister() {
      content.FirstRegister(); //&lt;-- content
  }
  </code></pre>