WPF应用启动问题 [英] WPF app startup problems

查看:60
本文介绍了WPF应用启动问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在满脑子都是想完全理解 Unity.所以我决定深入研究并开始将它添加到一个分支中,看看它会把我带到哪里.令人惊讶的是(或者可能不是),我只是让我该死的应用程序正确加载.

My brain is all over the map trying to fully understand Unity right now. So I decided to just dive in and start adding it in a branch to see where it takes me. Surprisingly enough (or maybe not), I am stuck just getting my darn Application to load properly.

似乎正确的方法是覆盖 App.cs 中的 OnStartup.我已从 App.xaml 中删除了我的 StartupUri,因此它不会创建我的 GUI XAML.我的 App.cs 现在看起来像这样:

It seems like the right way to do this is to override OnStartup in App.cs. I've removed my StartupUri from App.xaml so it doesn't create my GUI XAML. My App.cs now looks something like this:

public partial class App : Application
{
    private IUnityContainer container { get; set; }

    protected override void OnStartup(StartupEventArgs e)
    {
        container = new UnityContainer();
        GUI gui = new GUI();
        gui.Show();
    }

    protected override void OnExit(ExitEventArgs e)
    {
        container.Dispose();
        base.OnExit(e);
    }
}

问题是当我启动应用程序时没有任何反应!我在 container 赋值处放置了一个断点,它永远不会被命中.

The problem is that nothing happens when I start the app! I put a breakpoint at the container assignment, and it never gets hit.

我错过了什么?App.xaml 当前设置为 ApplicationDefinition,但我希望它能够工作,因为我正在查看一些示例 Unity + WPF 代码(来自 Codeplex) 做了完全相同的事情,只是它有效!

What am I missing? App.xaml is currently set to ApplicationDefinition, but I'd expect this to work because some sample Unity + WPF code I'm looking at (from Codeplex) does the exact same thing, except that it works!

我还通过单步启动了该应用程序,它最终到达了 App.xaml 中的第一行.当我进入这一行时,应用程序刚刚开始运行",但我什么也没看到(而且我的断点没有被击中).如果我在示例应用程序中做完全相同的事情,进入 App.xaml 会让我直接进入 OnStartup,这正是我期望发生的.啊!

I've also started the app by single-stepping, and it eventually hits the first line in App.xaml. When I step into this line, that's when the app just starts "running", but I don't see anything (and my breakpoint isn't hit). If I do the exact same thing in the sample application, stepping into App.xaml puts me right into OnStartup, which is what I'd expect to happen. Argh!

我还刚刚从头开始创建了一个新的 WPF 应用程序,删除了 StartupUri,覆盖了 OnStartup(),它也可以工作.什么?

I've also just created a new WPF application from scratch, removed StartupUri, overrode OnStartup(), and it also works. WTH?

将 Unity 构造放在我的 GUI 的 Window_Loaded 事件处理程序中是不是一件坏事?它真的需要在应用级别吗?

Is it a Bad Thing to just put the Unity construction in my GUI's Window_Loaded event handler? Does it really need to be at the App level?

推荐答案

仔细检查 App.xaml 中的 x:Class 是否与 App.xaml.cs 中的名称空间/类相同.很容易从另一个项目复制/粘贴并忘记修改它.

Double check that the x:Class in App.xaml in the same namespace/class as in your App.xaml.cs. It's easy to copy/paste from another project and to forget to modify this.

如果由于任何原因您无法解决此问题,请删除 App.xaml 并添加执行 new App().Run()Main()代码>.如果这也不起作用,那么这里真的很奇怪.

If for any reason you don't manage to solve this, remove the App.xaml and add a Main() which does a new App().Run(). If this doesn't work either, there's something really odd here.

这篇关于WPF应用启动问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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