Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open new window after first

Tags:

wpf

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

like image 794
Afnan Bashir Avatar asked Feb 19 '11 21:02

Afnan Bashir


People also ask

How do I open a new window in Chrome?

To open a new window, use a keyboard shortcut: Windows & Linux: Ctrl + n. Mac: ⌘ + n.

What does opens in a new window mean?

Why Open Links in a New Window? The main reason is simple: you want people to stay on your website. If a user clicks a link on your website and is taken off your site, there's not a great chance they'll take the time to navigate back. Sure, they may hit the Back button in their browser or retype your URL.

How do I get popups to open in a new window?

Open Link in a Popup Window In order to open them in a new window, we add target="_blank" attribute to links. However to open the links in a separate popup window, we can make use of the onclick property and specifying a inline JavaScript code window.

What is the difference between opening a new tab and opening a new window?

A tab is more or less same as a window. A window can contain several tabs and all session data and cookies are shared across all tabs and open window. It's better to open a lot of tabs than opening several windows because too many window becomes too cluttered to handle.


2 Answers

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.

like image 64
Luigi Avatar answered Oct 19 '22 08:10

Luigi


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.

like image 24
Chris Wenham Avatar answered Oct 19 '22 09:10

Chris Wenham