如何根据参数启动WPF [英] How to start WPF based on Arguments

查看:230
本文介绍了如何根据参数启动WPF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发做一些文件操作的应用程序,我希望能够通过控制台或通过UI做的操作(我选择了WPF)。



我非常想说:(伪)

 如果(Environment.GetCommandLineArgs()长度大于0) 
{
//不要打开WPF UI,而是做处理基于
//在
传递的参数}
,否则
{
//打开WPF UI
}

我读过约了几个不同的方法在开始WPF窗口/应用编程这样的:

 应用程序=新的应用程序(); 
app.Run(新窗口1());



但我不能完全肯定我只想将其插入一个控制台应用程序。



有没有人对我怎么能做到这一点的最佳做法或建议?主要处理功能是我创建了一个Helper类。所以基本上我不是一个静态的启动方法(如标准控制台应用程序创建)或用户界面来访问取决于传递的参数辅助类。


解决方案

在应用类有一个活动的启动,你可以使用它。它为您提供您通过命令提示符提供ARGS。




的App.xaml




 <应用X:类=WpfApplication99.App
的xmlns =HTTP://模式。 microsoft.com/winfx/2006/xaml/presentation
的xmlns:X =http://schemas.microsoft.com/winfx/2006/xaml
启动=App_Startup>




App.xaml.cs



 公共部分类应用:应用
{
无效App_Startup(对象发件人,StartupEventArgs E)
{
//应用程序正在运行
//处理命令行参数
布尔startMinimized = FALSE;
的for(int i = 0; i = e.Args.Length;!++我)
{
如果(e.Args [I] ==/ StartMinimized)
{
startMinimized = TRUE;
}
}

//创建应用程序主窗口,减少起动如果指定
主窗口的主窗口=新的主窗口();
如果(startMinimized)
{
mainWindow.WindowState = WindowState.Minimized;
}
mainWindow.Show();
}
}



我希望这会有所帮助。


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).

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:

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.

解决方案

In Application class there is an event "StartUp" you can use it . It provide you the args you provide through command prompt .

App.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">

App.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.

这篇关于如何根据参数启动WPF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆