Application.Startup事件限制(错误?) [英] Application.Startup event limitations (bug?)

查看:334
本文介绍了Application.Startup事件限制(错误?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它说,我可以使用 启动 事件对很多事情,比如初始化,创建多种形式,blablabla。

It says, I can use Startup event for many things, like initialization, creating multiple forms, blablabla.

不过,尝试创建一个新的WPF应用程序并添加此事件处理程序:

However, try to create a new WPF application and add this event handler:

    private void App_Startup(object sender, StartupEventArgs e)
    {
        Window window = new Window();
    }

和关闭主窗口之后,应用程序将在存储挂断。暂停在那一刻,在VS执行将下降丑陋的<击>崩溃调用堆栈有很多电话的地方远离我的源代码窗口。

And after closing main window your application will hang up in the memory. Pausing executing in VS at that moment will drop ugly crash call stack window with a lot of calls somewhere far away from my source code.

任何?想法

PS:我需要实例我所有的窗户自学配置目的的一次。我应该使用一些其他的地方吗?

P.S.: I need to instantiate all of my windows for self-learning configuration purpose once. Should I use some other place?

让我举一个更好的例子(上面的例子是很好的重现该问题,似乎很难理解我实际上做)。我删除的StartupUri ,然后:

Let me give a better example (example above is good to reproduce the problem, seems it's hard to understand what I am actually doing). I remove StartupUri and then:

    private void App_Startup(object sender, StartupEventArgs e)
    {
        // un-comment this line to reproduce the problem:
        // Window window = new Window();

        // actual implementation will be
        // Window1 window1 = new Window1();
        // Window2 window2 = new Window2();
        // Window3 window3 = new Window3();
        // Window4 window4 = new Window4();
        // ...

        // start main window as usually
        MainWindow mainWindow = new MainWindow();
        mainWindow.Show();
    }

和为什么需要窗口的实例(WINDOW1,..2。 3,...)?因为他们的内容会被检查,以创造对我要保存配置控制列表。有些窗口将绝不会显示给用户(示例,如果他不是管理员),他们中的一些弹出窗口,有些是编辑器等,所以我不想显示它们。但在任何应用程序启动时的配置,必须创建和保存。而我现在正在寻找这样做的地方。

And why do I need instances of windows (window1, ..2, ..3, ...)? Because their content will be inspected to create a list of controls for which I want to save configuration. Some of these windows will never be shown to the user (to example, if he is not admin), some of them are popups, some are editors, etc. So I do not want to display them. But at any application startup the configuration has to be created and saved. And I am looking now for the place to do so.

出人意料的是,使用专用的事件启动似乎有些问题的与创建多个窗口,但的不显示的他们。问题是,为什么和如何解决它。

Surprisingly, using dedicated event Startup seems have some problems with creating multiple windows but not displaying them. Question is why and how to solve it.

测试多一点。试试这个代码,并解释我为什么应用程序,而不显示的任何窗口关闭?

Testing a bit more. Try this code and explain me, why application is closed without showing any window?

    private void App_Startup(object sender, StartupEventArgs e)
    {
        Window window = new Window();
        window.Close(); // closing without opening window

        MainWindow mainWindow = new MainWindow();
        mainWindow.Show();
        //mainWindow = new MainWindow(); // can be un-commented, for absolutely no effect
        mainWindow.Show();
        mainWindow.Show();
        mainWindow.Show();
        mainWindow.Show(); // you will not see mainWindow at all, all Show doing nothing
    }



更​​多问题。 ?我该怎么办

More questions. What do I do?

东西是什么在起作用,但闻起来:

Something what works, but smells:

    private void App_Startup(object sender, StartupEventArgs e)
    {
        Window window = new Window();

        MainWindow mainWindow = new MainWindow();
        mainWindow.ShowDialog();
        Shutdown();
    }



通知,要求的ShowDialog (这将使得事件处理程序等待该窗口关闭),并调用关闭之后。

Notice, calling ShowDialog (which will make event handler waiting for that window closing) and calling Shutdown right after.

它仍然是尚不清楚什么是在启动事件处理程序的问题创造一些窗口的实例。任何想法?

It is still not clear what is the problem in the Startup event handler to create instances of some windows. Any ideas?

推荐答案

我有解决方案,它似乎相当不错。这类似于的WinForms - 不要在主一切

I have solution, which seems pretty good to me. Idea is similar to winforms - do everything in the Main.

不过,这是有点的棘手的在WPF(我用这个问题作为一个导游):

This, however, is a bit tricky in wpf (I used this question as a guide):


  • 删除的StartupUri 应用。 XAML ;

  • 设置的App.xaml 属性生成操作(这听起来很奇怪,但它适用于桌面应用程序)。这将从自动生成的类中删除办法( App.g.cs App.gics 的)。

  • 手动添加方法到应用

  • Remove StartupUri from App.xaml;
  • Set App.xaml property Build Action to Page (this sounds strange, but it works for desktop application). This will remove Main method from auto-generated classes (App.g.cs and App.g.i.cs).
  • Add Main method manually into Application:

public partial class App : Application
{

    [STAThread]
    public static void Main()
    {
        Window window1 = new Window();
        Window window2 = new Window();
        Window window3 = new Window();
        // ...

        MainWindow start = new MainWindow();
        start.ShowDialog();

        SomeOtherWindow next = new MainWindow();
        next.ShowDialog();

    }
}

现在我可以直接控制哪个窗口显示时(程序流控制),以及没有更多的错误有没有闭合,而不显示他们创建的窗口实例时应用。

Now I can directly control which window to show and when (program flow control), as well as there is no more bug with not-closing application when creating instances of windows without displaying them.

可疑事情是这样设置,这一事实,我不实例化应用,我也叫运行()。我不知道的是,如果这将是在将来的一个问题。将是不错的知道这是肯定的。

The suspicious things are this Page setting and that fact, what I do not instantiate Application, nor I call Run(). I do not know yet, if it will be a problem in the future. Would be nice to know it for sure.

这可能是必要的还是初始化程序(加载资源?)然后

It might be necessary to init application still (to load resources?), then

    [STAThread]
    public static void Main()
    {
        App app = new App();
        app.InitializeComponents();

        // ... the rest
        // possibly app.MainWindow = start; or app.MainWindow = next;
        // if only 1 window, then app.Run(new MainWindow());
    }

这篇关于Application.Startup事件限制(错误?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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