2

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?

ale
  • 3,301
  • 10
  • 40
  • 48
  • Do not create a Console application if you want to show a window. You need a message pump in order to do that. Start with the template to make sure it gets things right. Console applications are completely different. – Cody Gray - on strike May 20 '11 at 15:22
  • @Cody, there's no reason you can't show GUI from a console app. .NET supports this quite well. – Joe White May 20 '11 at 19:46
  • "Can", of course, is different from "should". There's a reason the two projects have different names: they have explicitly different design and architecture goals. It's possible to let the rich and versatile functionality of the .NET Framework become a hindrance, rather than a virtue. This may well be one of those cases. I'm suggesting to seriously reconsider whether this should be a console app in the first place, considering what you're asking to do, not suggesting that it's impossible. – Cody Gray - on strike May 21 '11 at 09:13
  • possible duplicate of [How Start a window with Main() in Console Proyect.??](http://stackoverflow.com/questions/6065618/how-start-a-window-with-main-in-console-proyect) – Cody Gray - on strike May 21 '11 at 11:43

2 Answers2

1

Just use the ShowDialog() method instead.

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

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

Marino Šimić
  • 7,318
  • 1
  • 31
  • 61
1

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);
}
Jay Zhu
  • 1,636
  • 13
  • 17