在控制台应用程序中启动 WPF 应用程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33794377/
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 13:32:40  来源:igfitidea点击:

Start WPF Application in Console Application

c#wpf

提问by king jia

Is it possible to start WPF Application in Console mode?

是否可以在控制台模式下启动 WPF 应用程序?

public partial class App : Application
{
    public App()
    {
        InitializeComponent();
    }
}

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

[STAThread]
static void Main(string[] args)
{
  if (args.Length > 0)
  {
     switch (args[0].ToLower())
     {
       case "/g": RunApplication(); break;
     }
  }
}

private static void RunApplication()
{
    var application = new System.Windows.Application();

    application.Run(new App());
}

It will show Argument type 'WPF.app' is not assignable to parameter type 'System.Windows.Window'.

它将显示参数类型“WPF.app”不可分配给参数类型“System.Windows.Window”。

Any solution to work around it?? Any different between

有什么解决方法可以解决它?之间有任何不同

1.public partial class App : Application

1.public 部分类 App : Application

2.public partial class App : Window

2.public 部分类 App : Window

回答by B.K.

You could declare a Window and then start your app this way:

您可以声明一个 Window,然后以这种方式启动您的应用程序:

var application = new System.Windows.Application();
application.Run(new Window());

EDIT:

编辑:

You seem a bit confused, so let me explain:

你似乎有点困惑,让我解释一下:

Say you have a program:

假设你有一个程序:

using System;

namespace ConsoleApplication
{
    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            RunApplication();
        }

        private static void RunApplication()
        {
            var application = new System.Windows.Application();
            application.Run();
        }
    }
}

This will run a WPF application with no Window.

这将运行一个没有窗口的 WPF 应用程序。

If, on the other hand, you pass a Window into application.Run(), you will get a WPF window. App should not derive from Window, since it should derive from Application.

另一方面,如果您将一个 Window 传递给 application.Run(),您将获得一个 WPF 窗口。App 不应该从 Window 派生,因为它应该从 Application 派生。

Application.Run method either takes no arguments or a Window. It does not take Application. Therefore, if you want to start a previously created Application, as you have over there, you should do something like this:

Application.Run 方法要么不接受任何参数,要么接受一个 Window。它不需要应用程序。因此,如果你想启动一个以前创建的应用程序,就像你在那里一样,你应该做这样的事情:

private static void RunApplication()
{
    var application = new App();
    application.Run();  // add Window if you want a window.
}

Lastly, if you want to just use application.Run()and not have to pass a specific Window, just declare a starting Window in your Application XAML using StartupUri:

最后,如果您只想使用application.Run()而不必传递特定窗口,只需使用 StartupUri 在应用程序 XAML 中声明一个起始窗口:

<Application x:Class="WPF.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         StartupUri="SomeWindow.xaml">
</Application>