Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从程序类开始打开窗口?_C#_Wpf_Interface_Console Application_3 Tier - Fatal编程技术网

C# 从程序类开始打开窗口?

C# 从程序类开始打开窗口?,c#,wpf,interface,console-application,3-tier,C#,Wpf,Interface,Console Application,3 Tier,我有一个控制台应用程序。所以我需要打开一个名为“UserInterface.xaml”的窗口,这是一个窗口 我有我的课程,我有: class Program { [STAThread] static void Main(string[] args) { var userInterface = new UserInterface(); userInterface .Show(); }

我有一个控制台应用程序。所以我需要打开一个名为“UserInterface.xaml”的窗口,这是一个窗口

我有我的课程,我有:

class Program
{
        [STAThread]
        static void Main(string[] args)
        {        
            var userInterface = new UserInterface();
            userInterface .Show();
}
问题是UserInterface.xaml打开后立即关闭。我需要它从用户那里获取一些数据

这是我的类用户界面:

public partial class UserInterface: Window
    {       

        public UserInterface()
        {                
            InitializeComponent();
        }

........
}
如何使UserInterface窗口保持打开状态?

只需使用ShowDialog()方法即可

    UserInterface userInterface  = new UserInterface();
    userInterface.ShowDialog();
它将一直阻塞,直到手动或编程关闭窗体。

尝试按如下方式优化Main():

[STAThread]
static void Main(string[] args)
{
    var userInterface  = new UserInterface();

    System.Windows.Application app = new System.Windows.Application();
    app.Run(userInterface);
}

如果要显示窗口,请不要创建控制台应用程序。要做到这一点,您需要一个消息泵。从模板开始,确保它能够正确地完成任务。控制台应用程序完全不同。@Cody,没有理由不能从控制台应用程序中显示GUI。NET很好地支持了这一点,“可以”当然不同于“应该”。这两个项目有不同的名称是有原因的:它们有明显不同的设计和架构目标。可能会让.NET Framework丰富多彩的功能成为一种障碍,而不是一种美德。这很可能就是其中之一。我建议首先认真地重新考虑这是否应该是一个控制台应用程序,考虑你的要求,而不是说这是不可能的