如果我的应用程序未设置 StartupUri,则不会解析 App.xaml 文件? [英] App.xaml file does not get parsed if my app does not set a StartupUri?

查看:59
本文介绍了如果我的应用程序未设置 StartupUri,则不会解析 App.xaml 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景:我正在使用 MVVM 创建 WPF 应用程序,并使用 DI 容器构建我的 ViewModel

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

我的 App.xaml 如下所示:

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 如下所示:

MainWindow.xaml looks like this:

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

现在,这一切正常,但 StartupUri 被硬编码到 XAML 中,这是我不想要的.
按照我找到的几篇博文和文章的指导,我删除了 StartupUri,并尝试通过在 App.xaml 中挂接 OnStartup 来创建 MainWindow.cs,像这样:

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:

找不到名为{serviceLocator}"的资源.资源名称区分大小写.标记文件WpfApp;component/mainwindow.xaml"第 8 行位置 45 中的对象System.Windows.Data.Binding"出错.

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.

据我所知, 部分根本没有从 xaml 文件中读出.我可以在 OnStartup 中放入一些代码来像这样以编程方式添加资源:

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();

然而,这是一个丑陋的黑客,如果我想稍后在 app.xaml 文件中放入其他内容,这对我没有帮助:-(

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?

推荐答案

与其覆盖 OnStartup,不如尝试使用事件:

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>

背后的代码:

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();
    }
}

这篇关于如果我的应用程序未设置 StartupUri,则不会解析 App.xaml 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆