1

I have a console project, but now I need to put an user interface on. So I'm using the 3 tier model (presentation, business, access data).

In my method Main() I call to presentation layer (like app in Window form or Wpf), so, in the presentation layer is the interaction with user through CONSOLE.

Now I add a window called "UserInterface.xaml" in the presentation layer to use instead of console. Because should be with INTERFACE not console.

I have observed that in MainWindow the called is with MainWindow.Show();

But I don't know how to call my "UserInterface.xaml", because has no .Show() method.

This is my method Main:

public static void Main()
{
  MainWindow.Show(); // THIS IS WITH MainWindow.xaml
  UserInterface.???  // THIS IS MY CASE WITH UserInterface.xaml
}

So can somebody tell me how I can call my window from the Main method??

Kevin Worthington
  • 457
  • 2
  • 5
  • 17
ale
  • 3,301
  • 10
  • 40
  • 48

2 Answers2

5

You definitely got started with the wrong project template. To make a WPF window visible and interactive, you have to follow the rules for a UI thread. Which includes marking the main thread of your app as an STA thread and pumping a message loop. Like this:

class Program {
    [STAThread]
    public static void Main() {
        var app = new Application();
        app.Run(new MainWindow());
    }
}

Beware that Application.Run() is a blocking call, it will not return until the user closes the main window. This is a rather inevitable consequence of the way Windows works.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
1

Assuming UserInterface is really a window, this should work:

var window = new UserInterface();
window.Show();
svick
  • 236,525
  • 50
  • 385
  • 514
  • not recognizing the .Show();... and UserInterface is a Window. – ale May 19 '11 at 23:15
  • You are going to have to show us more code in that case, because if `UserInterface` were a `Window`, it would have a `Show()` method. How does the source code for `UserInterface` look like? – svick May 19 '11 at 23:28
  • this is part of the class UserInterface, how you see is a normal window: public class UserInterface: Window { public event EventHandler OptionSelected; public void SetPathCharger2() { InitializeComponent(); } } – ale May 20 '11 at 13:40
  • @ale, then `UserInterface` *does have* `Show()` method, unless `Window` is not `System.Windows.Window` in your code. – svick May 20 '11 at 14:42