Open a Window since Programm class?

扶醉桌前 提交于 2019-12-12 17:54:52

问题


I have a console application. So I need Open a Window called "UserInterface.xaml" this is a Window.

I my class Program I have this:

class Program
{
        [STAThread]
        static void Main(string[] args)
        {        
            var userInterface = new UserInterface();
            userInterface .Show();
}

The problem is when the UserInterface.xaml is opened but then is closed immediately. And I need it for capture some data from user.

this is my class UserInterface:

public partial class UserInterface: Window
    {       

        public UserInterface()
        {                
            InitializeComponent();
        }

........
}

How can I make the UserInterface window stay opened?


回答1:


Just use the ShowDialog() method instead.

    UserInterface userInterface  = new UserInterface();
    userInterface.ShowDialog();

It will block until the form is manually or programmatically closed.




回答2:


Try to refine your Main() as below:

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

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


来源:https://stackoverflow.com/questions/6073658/open-a-window-since-programm-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!