English 中文(简体)
WPF的申请在开始之前显示方言时即刻退出。
原标题:WPF application exits immediately when showing a dialog before startup

<>Update:I guess,我需要了解什么是“校正”的“支持”方式,以便在申请开始之前,显示方言。

该法典:

    public partial class App : Application
    {
        [STAThread]
        public static void Main()
        {
            var app = new App();
            app.InitializeComponent();

            new DialogWindow().ShowDialog();

            app.Run( new MainWindow() );
        }
    }

The DialogWindow shows up as expected.
But after closing it, the application exits immediately. MainWindow doesn t show up at all!

我做了一些细致的工作,并追踪了这一问题:

  1. When the dialog is created, it becomes app s MainWindow, since there is no MainWindow at the moment.
  2. Therefore, closing the dialog causes the application to post ShutdownCallback on the dispatcher queue.
  3. However, the dispatcher doesn t run long enough to execute the callback.
  4. Therefore, once app.Run is called subsequently, the first thing on the queue is ShutdownCallback, which, naturally, causes the app to close immediately.

根据这一分析,在以下几个方面存在着明显的工作:在<条码>后创建/code>,从而使其成为<条码> 适用<>/条码>。

www.un.org/Depts/DGACM/index_french.htm

首先,我想像我的 d。 我指的是,没有明确的理由按此顺序建立窗口,我只是通过一些偷窃找到了。 这不能成为支持的方法。

第二,这显然是ug。 我指的是,如果在关闭后建立第二个窗口得到明确的支持,那么它就应当把一些“代号”丢掉。

第三,这不仅是一个ug子,而且它看起来像一个非常冷静的东西,像一个多面的开端人那样。

www.un.org/Depts/DGACM/index_spanish.htm 所有这一切都使我相信,也许我在此没有基本的东西? 难道我根本不会感觉到吗? 是否应该以某种不同的方式来做到这一点?

Here s some background:
The application has to do some bootstrapping on startup. Check this and that, setup exception handlers, logging - you know, the usual stuff. In this process, it may become necessary to ask the user for some help - which is what the dialog is for.

我绝对不想在<代码>上安装某种类型的国家机器。 MainWindow.IsVisibleChanged or some such as that. 我愿保持真实的简单、简短和直截了当——应该采用boot锁法,以便用赤.的眼光轻易发现 b。

问题回答

在缺席的情况下,WPF申请的ShutdownMode是On LastWindowClose。 你的代码显示一个窗口,然后关闭。 因此,最后窗口关闭,申请关闭。 然后,在关闭时,你又展示了另一个窗口。 由于申请被关闭,窗口立即关闭。

因此,一切工作都是由你设计和规划的。

然而,你们想要做的是: 你的窗口首先显示,唯一的窗口应当是“特殊窗口”,关闭后,你希望继续实施,展示你的“主要窗口”,然后在申请(或所有与 app有关的窗口)关闭后退出申请。

最容易的方式是:首先将关闭模式定在“ExplicitShutdown”,然后向“LastWindowClose”或“MainWindowClose”展示主要窗口。 法典:

public static void Main()
{
    var app = new App();
    app.InitializeComponent();

    app.ShutdownMode = ShutdownMode.OnExplicitShutdown;
    new DialogWindow().ShowDialog();

    var mainWindow = new MainWindow();
    app.MainWindow = mainWindow;
    app.Run(mainWindow);
    // When the window has loaded, it should then set the app.ShutdownMode to what you actually want.
}

EDIT: I am not sure what exactly you are doing. The code you gave will not compile, since when properly using a WPF application class (with an App.xaml build-action as ApplicationDefinition), a Main method is already defined. If you just have a class derived from Application, you have no InitializeComponent() method. The only way to get you code to compile was by manually changing the build-action to Page. However, in that case, Application.Current == app.

因此,情况如下:

  1. The application starts. Since no WPF-application has been created so far, Application.Current is null. This also means no dispatcher-loop is running and dispatcher messages are unhandled (note that the dispatcher loop also handles windows messages).
  2. A new App-object is created. Since Application.Current is null, it sets itself as Application.Current.
    • Application.Current.MainWindow is null and Application.Current.Windows is an empty list.
    • Since ShutdownMode is OnLastWindowClose, once the last window of the current application (i.e. app) closes, shutdown starts.
  3. The DialogBox is shown modally. Since no dispatcher-loop is running, the ShowDialog() itself runs a "local" dispatcher-loop.
    • Actually this is two parts: First the window is created. It belongs to the current application, so it adds itself to Application.Current.Windows. Since it is the first window shown and Application.Current.MainWindow is null, it also sets itself as main window. Secondly, the window is shown modally.
    • Since Application.Current.Windows is now non-empty, once it is empty, shutdown will start.
  4. The user closes the dialog window. As part of being closed, the window removes itself from Application.Current.Windows. Also, since it is the MainWindow, this is set to null. Since Application.Current.Windows is now empty, shutdown starts. However, since there is no dispatcher-loop running, nothing is done yet (only an internal flag or similar is set).
    • If you had used app.Run(new DialogWindow()); app.Run(new MainWindow());, you would have an exception while creating the MainWindow, since in this case the dispatcher-loop is running properly. Thus, it can actually shutting itself down, so when the MainWindow is created, it throws an exception since the dispatcher-loop is already shut down.
  5. MainWindow is created. As above, it adds itself to Application.Current.Windows and sets itself as Application.Current.MainWindow.
    • However, the condition for shutting down the application has already been reached. But, so far, the application had no chance to do something.
  6. Now Run() is called. The dispatcher-loop starts again and now has a chance to shutdown the application. So it shuts down the application and closes any open windows.

同样,没有 b。

因此,解决这一问题的一个途径是改变为公开出口。 然后,第4步就没有达到关闭的理由。 更好的(更像通常的WPF申请一样)是确定适当的申请。 将启动Uri从App.xaml移走,而是处理启动活动:

private void OnStartup(object sender, StartupEventArgs e)
{
    this.ShutdownMode = ShutdownMode.OnExplicitShutdown;
    new DialogWindow().ShowDialog();

    var mainWindow = new MainWindow();
    this.ShutdownMode = ShutdownMode.OnLastWindowClose; // or OnMainWindowClose
    mainWindow.Show();
}

由于我们在关闭了方言窗口的同时,已开始实行直接关闭,因此没有理由申请开始关闭。 然后,在创立了MainWindow之后,我们又有一个窗口作为主要窗口和(其中一个)应用窗口。 因此,我们可以转向我们实际上想要的关闭模式,并展示主要窗口。

这似乎有 b。

我通常不把任何东西放到Main(>上,但我要说的是,No-arg app.Run()上,并说了我在方法上所需要的一切,但这不会改变你所看到的行为。

电话 在我准备展示主要应用窗口之前,我需要展示一些东西,如收集投入或显示间歇,我是第二线。

protected override void OnStartup(StartupEventArgs e)
{
    base.OnStartup(e);

    // show splash
    var thread = new Thread(() =>
    {
        Dispatcher.CurrentDispatcher.BeginInvoke
            ((Action)(() => new MySplashWindow().Show()));
        Dispatcher.Run();
    }
    thread.SetApartmentState(ApartmentState.STA);
    thread.IsBackground = true;
    thread.Start();

    // run configuration steps

    // instantiate and show main window
}

显然,如果你在二读时重新打上了<条码>ShowDialog(),那么你需要确保你在显示主窗之前得到答复。 这确实有好处,在等待用户投入时,你可以承担其他制约任务;这确实取决于你的各项任务是如何先后进行的。

或许不是——只是一个想法。

感谢所有捐助人提供非常丰富的职位。 我使用的是一只小船窗把它放在主要窗户上,但没有显示。 我还为OnLastWindow建立了关闭模式,当时,我打开并关闭了所有的方言,用真正的主窗更换了皮博窗,叫App.Run()。 这可能不是一个最佳做法,但它是行之有效的,它迅速运作。

Application app = new App();

MainWindow y = new MainWindow();
app.MainWindow = y;
y.WindowStartupLocation = WindowStartupLocation.CenterScreen;
app.ShutdownMode = ShutdownMode.OnLastWindowClose;
//do lots of setup work to include authentication
MainWindow x = new MainWindow(containerdata)
app.MainWindow = x;
App.Run()

我也有类似的问题。 我花了很多时间,无法解决问题。 我没有时间。

我不知道这是一个正确的解决办法,但我只是对原方言进行 h击,而不是予以关闭。 它仍在工作。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...