WPF app.run is not working?

I am only starting to learn WPF so please bear with me if this problem is so simple, yet I'm unable to figure out what is really happening.

What I'm trying to accomplish is to show a LoginWindow first then after a successful authentication the MainWindow will be displayed. I did not use the default App.xaml that is automatically created by Visual Studio, instead I created a custom application class. The Login works perfectly fine, but the app.Run(win) is not working, it doesn't show my MainWindow and the application exits immediately. Is there something I'm missing here? I have a winform application with almost same start up class, and it works perfectly fine. So I don't know why this approach is not working in WPF.

public class Startup
{
    [STAThread()]
    static void Main()
    {
        Application app = new Application();
        WinLogin login = new WinLogin();
        bool showLogin = true;
        while (showLogin)
        {
            login.ShowDialog();
            if (login.DialogResult.HasValue && login.DialogResult == true)
            {
                MainWindow win = new MainWindow();
                app.Run(win);
                if (!win.LogOff)
                    showLogin = false;
            }
            else
                showLogin = false;
        }
    }
}

Answers


After you have shown your dialog, try setting

app.MainWindow = win;

just before calling Application.Run(). I guess your first dialog was automatically set as main window, thus shutting down the application after it was closed.

Also, move the MainWindow creation directly after your application creation.


Need Your Help

Hue installation issue

django hadoop django-models hue python-huey

I am new to bigdata technologies/hadoop ecosystem.

SQL: Can I negate a condition in a where clause?

sql condition where-clause

I want to check if a boolean is true, then decide in the WHERE clause what condition to use.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.