wpf Windows 演示表单应用程序中的启动代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18138934/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:23:54  来源:igfitidea点击:

Startup Code in a Windows Presentation Form Application

wpfvb.net

提问by emufossum13

I'm working on a program that requires a file be loaded on program startup. I have seperate code for loading the file, but I've been using some basic methods to try and figure out a way to run code on the startup of a Windows Presentation Form Application. At the moment, I'm just trying to run a MsgBox function on the startup of this application. But I can't figure out how to do so.

我正在开发一个需要在程序启动时加载文件的程序。我有用于加载文件的单独代码,但我一直在使用一些基本方法来尝试找出一种在 Windows 演示表单应用程序启动时运行代码的方法。目前,我只是想在这个应用程序启动时运行一个 MsgBox 函数。但我不知道该怎么做。

回答by ta.speot.is

From the fine manual:

精美的手册

Application.Startup Event

Occurs when the Run method of the Application object is called.

应用程序启动事件

在调用 Application 对象的 Run 方法时发生。

Example XAML:

示例 XAML:

<Application
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  x:Class="SDKSample.App"
  Startup="App_Startup" />

Example C#:

示例 C#:

using System.Windows; // Application, StartupEventArgs, WindowState

namespace SDKSample
{
    public partial class App : Application
    {
        void App_Startup(object sender, StartupEventArgs e)
        {
            // Application is running 
            // Process command line args 
            bool startMinimized = false;
            for (int i = 0; i != e.Args.Length; ++i)
            {
                if (e.Args[i] == "/StartMinimized")
                {
                    startMinimized = true;
                }
            }

            // Create main application window, starting minimized if specified
            MainWindow mainWindow = new MainWindow();
            if (startMinimized)
            {
                mainWindow.WindowState = WindowState.Minimized;
            }
            mainWindow.Show();
        }
    }
}