Afnan Bashir
Afnan Bashir

Reputation: 7419

Open new window after first

How can this be done

Login window appears first and if every thing is fine just close login window and open second Main window. in win forms we modify program.cs but in wpf there is no program.cs.

Any solutions.?

Actully i did most of the work in the window that is created By default and now want to make it secondary(mean it should appear and then close when wanted giving control to new window)

   <Application x:Class="DevnMark_V1._0.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             Startup="Application_Startup">

    <Application.Resources>

    </Application.Resources>
</Application>



 public partial class App : Application
    {
        private void Application_Startup(object sender, StartupEventArgs e)
        {
            var login = new MainWindow();
            login.ShowDialog();
            if (myAppSett.Default.validated == true)
            {
            var mainWindow = new DevNMarkMainWindow();              
                mainWindow.ShowDialog();
            }
        }

Login Window start XML

<Window x:Class="DevnMark_V1._0.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
        xmlns:local="clr-namespace:Progress"
        Title="MainWindow" Height="292" Width="563" WindowStyle="None" BorderBrush="#FF0A6277" AllowsTransparency="True" WindowStartupLocation="CenterScreen" Topmost="True">

Exception occurs when i close Login window and occurs at point InitializeComponent();of second window when it is viewed when it is going to be initilized

Upvotes: 9

Views: 14132

Answers (3)

Luigi
Luigi

Reputation: 81

I solved this problem in this way:

  1. I removed from App.xaml the StartupUri="MainWinodw.xaml", leaving only Startup="Application_Startup".

  2. In Application_Startup, I IMMEDIATELY referenced both login and main windows:

    loginwindow Login = new loginwindow();
    mainwindow Main = new mainwindow();
    
  3. I verified my Login, then closed the login window and opened the main window with a simple .Show():

    Login.ShowDialog();
    if (!Login.DialogResult.HasValue || !Login.DialogResult.Value)
    {
        Application.Current.Shutdown();
    }
    
    main.Show();
    

No changes in ShutdownMode.

Upvotes: 8

Stonehead
Stonehead

Reputation: 376

The proposed OnExplicitShutdown method works and you can avoid explicitly shutting the app down in the second window simply by opening it with ShowDialog followed by this.Shutdown(), all in App.xaml thus not interfering with the rest of the application.

Upvotes: 0

Chris Wenham
Chris Wenham

Reputation: 24017

There may be no program.cs, but there is an App.xaml.cs in the default WPF program template and you can do the same thing there.

What you want to do is remove StartupUri="LoginWindow.xaml" from App.xaml and then modify App.xaml.cs's constructor to invoke your login window and your main window, like this:

public App() : base() {
    bool authenticated = false;
    LoginWindow login;
    while (!authenticated)
    {
        login = new LoginWindow();
        login.ShowDialog();
        authenticated = ValidUser(login.username, login.password);
    }

    MainWindow main = new MainWindow(login.username);
    main.ShowDialog();
}

The above example assumes you've added username and password as public properties to LoginWindow, and that you've modified MainWindow's constructor to take a parameter.

Upvotes: 4

Related Questions