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

后端 未结 4 2055
没有蜡笔的小新
没有蜡笔的小新 2020-11-30 01:07

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

My App.xaml looks like this:



        
相关标签:
4条回答
  • 2020-11-30 01:28

    To get around this know bug you can also just add resources programmatically.

    var rd = new ResourceDictionary()
    rd.Source = new Uri("pack://application:,,,/Resources;component/Colors.xaml");
    Resources.MergedDictionaries.Add(rd);
    

    This code can be placed inside the construtor for the App class.

    0 讨论(0)
  • 2020-11-30 01:40

    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();
        }
    }
    
    0 讨论(0)
  • 2020-11-30 01:45

    The most simple workaround is the definition of a x:Name:

    <Application x:Name="App" ...
        <Application.Resources>
            ...
        </Application.Resources>
    </Application>
    

    More information: http://connect.microsoft.com/VisualStudio/feedback/details/472729/wpf-cannot-find-resource-defined-in-the-app-xaml-file

    0 讨论(0)
  • 2020-11-30 01:51

    I've run into a similar/this same issue. There's a VS code generation bug where the code necessary to connect <Application.Resources> to the rest of the program sometimes is not inserted when <Application.Resources> contains only one entry and does not have a StartupUri attribute.

    Details: http://bengribaudo.com/blog/2010/08/19/106/bug-single-application-resources-entry-ignored (disclaimer--link points to my blog)

    0 讨论(0)
提交回复
热议问题