C# 如何基于 Arguments 启动 WPF
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11769113/
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
How to start WPF based on Arguments
提问by Tada
I'm currently developing an application that does some file manipulation and I want to be able to do the manipulation through the console or via an UI (I chose WPF).
我目前正在开发一个执行一些文件操作的应用程序,我希望能够通过控制台或 UI(我选择 WPF)进行操作。
I pretty much want to say: (psuedo)
我很想说:(伪)
if ( Environment.GetCommandLineArgs().Length > 0 )
{
//Do not Open WPF UI, Instead do manipulate based
//on the arguments passed in
}
else
{
//Open the WPF UI
}
I've read about a few different ways of starting the WPF Window/application programmatically like:
我已经阅读了一些以编程方式启动 WPF 窗口/应用程序的不同方法,例如:
Application app = new Application ();
app.Run(new Window1());
But I'm not entirely sure I want to just plug this into a Console Application.
但我不完全确定我想将它插入到控制台应用程序中。
Does anyone have best practices or recommendations on how I can achieve this? The main processing functionality is in a Helper class I created. So basically I either want a static start method (like standard Console Application creates) or the UI to access the Helper class depending on the arguments passed in.
有没有人有关于我如何实现这一目标的最佳实践或建议?主要处理功能在我创建的 Helper 类中。所以基本上我想要一个静态启动方法(如标准控制台应用程序创建)或 UI 来访问 Helper 类,具体取决于传入的参数。
采纳答案by yo chauhan
In Applicationclass there is an event "StartUp" you can use it . It provide you the args you provide through command prompt. Here is an example from MSDN:
在Application课堂上有一个事件“ StartUp”你可以使用它。它为您提供您通过命令提示符提供的参数。这是来自MSDN的示例:
App.xaml
应用程序.xaml
<Application x:Class="WpfApplication99.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Startup="App_Startup">
</Application>
App.xaml.cs
应用程序.xaml.cs
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();
}
}
I hope this will help.
我希望这将有所帮助。
回答by Habeeb
There are 2 options to get the command line arguments
1) If you want to read the arguments OnStartup. This is good for global access of the args.
有 2 个选项可以获取命令行参数
1) 如果您想读取参数OnStartup。这有利于args.
Override OnStartupin App.xaml.csand look at the Argsproperty of the StartupEventArgsclass.
覆盖OnStartup的App.xaml.cs和看Args的财产StartupEventArgs类。
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
foreach (string arg in e.Args)
{
// TODO: whatever
}
base.OnStartup(e);
}
}
2) Another easy way is to read the arguments from the Environment Object.
2)另一种简单的方法是从环境对象中读取参数。
Environment.GetCommandLineArgs();
This can be used from anywhere in the application like from the Form / Page also.
这可以从应用程序的任何地方使用,例如从表单/页面也可以使用。

