0

希望这是有道理的。

我有这样的事情:

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

            if (!Directory.Exists(dataFolder))
            {
                Directory.CreateDirectory(dataFolder);
            }

            try
            {
               using (DataContext context = new DataContext())
                   {
                        context.Database.CreateIfNotExists();
                   }

            }
            catch (IOException ex)
            {

            }

            KeyProgram.Show();

            if (Manager.KeyExists == true)
            {
            MainWindowViewModel viewModel = new MainWindowViewModel();

            this.MainWindow = new MainWindow();
            this.MainWindow.DataContext = viewModel;
            this.ShutdownMode = System.Windows.ShutdownMode.OnMainWindowClose;

            Helper.WindowDialogService.SetOwner(this.MainWindow);

            viewModel.Init();

            this.MainWindow.Show();

            }
            else {

                Console.WriteLine("Please try again"); 

            } 

        }

在显示我的 licensekey 窗口后,我想中断以便系统需要用户输入(让用户输入许可证密钥)然后继续运行 if-else 语句(在 if (LicenseKeyManager.licenseKeyExists == true))。

然而,目前,onStartup,应用程序只是首先运行所有代码,然后如果我输入密钥并验证它,它不会运行该 if 语句,因为它已经运行了。

在继续执行该 if 语句之前,如何从视图中中断用户输入?

现在在 LicenseKeyProgram.Show() 之后,如果我在 if 语句处设置断点,应用程序将不会让用户输入任何内容,因为它卡在加载中(无法在窗口上执行任何操作)。

我在这里需要一个事件处理程序还是...?

4

1 回答 1

4

使用 ShowDialog 而不是 Show。

相关MSDN链接:

http://msdn.microsoft.com/en-us/library/system.windows.window.showdialog%28v=vs.110%29.aspx


哦,等等,还有一个问题,我可以获得用户输入,但应用程序从未运行/点击 if-else 语句?为什么?

您必须在调用另一个窗口上的 showdialog 之前设置主窗口,否则它将触发应用程序的关闭(因为当所有窗口都关闭时应用程序将关闭)。

例子:

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

    MainWindow window = new MainWindow();

    Window1 test = new Window1();
    test.ShowDialog();

    if (test.InvalidLicense)
    {
        Shutdown();
        return;
    }

    window.Show();
}

资源:

WPF 在主窗口之前显示对话框

http://www.ageektrapped.com/blog/the-wpf-application-class-overview-and-gotcha/

于 2014-08-08T19:50:47.353 回答