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;
}
开发者_C百科 else
showLogin = false;
}
}
}
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.
精彩评论