title: App.xaml file does not get parsed if my app does not set a StartupUri?
body: <p>Background: I'm creating a WPF app using MVVM, and using a DI container to build my ViewModels</p>
  
  <p>My App.xaml looks like this:</p>
  
  <pre><code>&lt;Application x:Class="WpfApp.App"
      ...xmlns etc...
      StartupUri="MainWindow.xaml"&gt;
      &lt;Application.Resources&gt;
          &lt;app:ServiceLocator x:Key="serviceLocator" /&gt;
      &lt;/Application.Resources&gt;
  &lt;/Application&gt;
  </code></pre>
  
  <p>MainWindow.xaml looks like this:</p>
  
  <pre><code>&lt;Window x:Class="CompositeMefWpfApp.MainWindow"
      ...xmlns etc... &gt;
      &lt;Control.DataContext&gt;
          &lt;Binding Path="MainWindowViewModel" Source="{StaticResource serviceLocator}" /&gt;
      &lt;/Control.DataContext&gt;
  </code></pre>
  
  <p>Now, this all works fine, but the <code>StartupUri</code> is hardcoded into the XAML, which I don't want.<br>
  Following guidance of several blogposts and articles I found, I removed the <code>StartupUri</code>, and tried to create the <code>MainWindow</code> by hooking <code>OnStartup</code> in App.xaml.cs, like this:</p>
  
  <pre><code>protected override void OnStartup( StartupEventArgs e )
  {
      base.OnStartup(e);
      new MainWindow().Show();
  }
  </code></pre>
  
  <p>The problem is, the app crashes when trying to show the window, with this exception:</p>
  
  <blockquote>
    <p>Cannot find resource named '{serviceLocator}'. Resource names are case sensitive.  Error at object 'System.Windows.Data.Binding' in markup file 'WpfApp;component/mainwindow.xaml' Line 8 Position 45.</p>
  </blockquote>
  
  <p>As far as I can tell, the <code>&lt;Application.Resources&gt;</code> section is simply not being read out of the xaml file. I can put some code in the OnStartup to add the resource programatically like this:</p>
  
  <pre><code>Resources.BeginInit();
  Resources.Add("serviceLocator", new ServiceLocator());
  Resources.EndInit();
  </code></pre>
  
  <p>However that's an ugly hack, and doesn't help me if I wanted to put something else in the app.xaml file later on :-(</p>
  
  <p>Should I be hooking some other event? Is there a way around this?</p>