Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

App.xaml file does not get parsed if my app does not set a StartupUri?

Background: I'm creating a WPF app using MVVM, and using a DI container to build my ViewModels

My App.xaml looks like this:

<Application x:Class="WpfApp.App"     ...xmlns etc...     StartupUri="MainWindow.xaml">     <Application.Resources>         <app:ServiceLocator x:Key="serviceLocator" />     </Application.Resources> </Application> 

MainWindow.xaml looks like this:

<Window x:Class="CompositeMefWpfApp.MainWindow"     ...xmlns etc... >     <Control.DataContext>         <Binding Path="MainWindowViewModel" Source="{StaticResource serviceLocator}" />     </Control.DataContext> 

Now, this all works fine, but the StartupUri is hardcoded into the XAML, which I don't want.
Following guidance of several blogposts and articles I found, I removed the StartupUri, and tried to create the MainWindow by hooking OnStartup in App.xaml.cs, like this:

protected override void OnStartup( StartupEventArgs e ) {     base.OnStartup(e);     new MainWindow().Show(); } 

The problem is, the app crashes when trying to show the window, with this exception:

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.

As far as I can tell, the <Application.Resources> 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:

Resources.BeginInit(); Resources.Add("serviceLocator", new ServiceLocator()); Resources.EndInit(); 

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 :-(

Should I be hooking some other event? Is there a way around this?

like image 606
Orion Edwards Avatar asked Feb 12 '09 21:02

Orion Edwards


People also ask

How do I add apps to XAML?

Solution 1. Go to add new items -> online templates and then choose 'Silverlight Application Class'. This will add a new app. xaml for you.

How do I create a XAML file?

To begin editing your first XAML file, use Visual Studio or Visual Studio for Mac to create a new Xamarin. Forms solution. (Select the tab below corresponding to your environment.) In the Configure your new project window, set the Project name to XamlSamples (or whatever your prefer), and click the Create button.

What is app XAML used for?

What is XAML? Extensible Application Markup Language (XAML) is the language used to define your app's user interface. It can be entered manually, or created using the Visual Studio design tools.

What is XAML in C #?

XAML is a new descriptive programming language developed by Microsoft to write user interfaces for next-generation managed applications. XAML is the language to build user interfaces for Windows and Mobile applications that use Windows Presentation Foundation (WPF), UWP, and Xamarin Forms.


1 Answers

Rather than overriding OnStartup, try using an event instead:

<Application x:Class="My.App"     xmlns="..."     Startup="Application_Startup"     ShutdownMode="OnExplicitShutdown">         <Application.Resources>             <app:ServiceLocator x:Key="serviceLocator" />         </Application.Resources>     </Application> 

Code behind:

public partial class App : Application {     public App()     { }     private void Application_Startup(object sender, StartupEventArgs e)     {         // TODO: Parse commandline arguments and other startup work          new MainWindow().Show();     } } 
like image 57
Nidonocu Avatar answered Oct 07 '22 16:10

Nidonocu