自定义WPF对话框窗口导致shell窗口在.Show调用后立即关闭

时间:2013-11-18 21:52:57

标签: wpf windows c#-4.0 modal-dialog prism-4

行。一点设置。

我有一个使用Unity的基于PRISM的小应用程序。

我创建了一个类(与shell窗口所在的主程序分开),它包含自己的WPF窗口,在更新运行时弹出对话框。

该类进行一些文件复制(真正简单的东西就像PoC一样),窗口会更新用户的进度。

窗口以模态显示(.ShowDialog();),以便在更新运行时阻塞线程。

更新是在ContentRendered事件中运行的。

更新完成后,在窗口上调用.Close()。

在主程序的App.xaml.cs文件的OnStartup事件中,实例化此类并启动更新过程(通过调用名为StartUpdate()的类的公共函数)。

之后,创建并运行一个引导程序。

最后,在bootstrapper类中,加载了一些模块(除了注册视图外,它们不执行任何操作),shell窗口被解析并显示在InitializeShell的覆盖中。

所有非常标准的东西,除了程序在退出InitializeShell后关闭。如果我没有在updater类中调用.Close并让用户用X关闭自定义对话框,则会发生同样的事情。我也尝试以非模态的方式显示对话框(只是.Show()),程序的其余部分工作正常。但是,该过程不会阻止,因此更新将在加载主程序之后或之后发生。这将毫无意义。我找到了一些方法。我只是好奇是否有人能解释这种行为。

编辑:示例代码段: 当用户关闭Window1窗口时,MainWindow永远不会加载。

/// <summary>
/// Interaction logic for App.xaml. This is where the program starts.
/// </summary>
public partial class App : Application
{
    private void Application_Startup(object sender, StartupEventArgs e)
    {
        dialogWPF.Class1 c = new dialogWPF.Class1();

        c.Update();

        //After this, the program should continue as normal (Open the MainWindow window)
    }
}

/// <summary>
/// This is the code for Class1 that is called in App.xaml.cs
/// </summary>
public class Class1
{
    private Window1 _win;

    public Class1()
    {
        _win = new Window1();
    }

    public void Update()
    {
        _win.ShowDialog();
    }



}

1 个答案:

答案 0 :(得分:0)

只是猜测:你是否将shell窗口设置为应用程序主窗口,如下所示:

Application.Current.MainWindow = shell;
InitializeShell()覆盖中的

?否则,您的更新对话框可能是应用程序主窗口,并导致应用程序在关闭后退出。