Frage

My app's main UI is in MainWindow.xaml.

Firstly - what is causing this to be the window the application opens on startup. It does not appear to be defined as a "starup object" and there does not appear to be any code that specifically launches this window.

I can make my login window appear when the app starts by in the loaded event of MainWindow.xaml defining a new "login.xaml" and telling it to show as a dialog. However, if I do this then MainWindow does not appear until Login has been closed.

What I want to achieve is when my app starts, the MainWindow appears and then on top of that the Login window is displayed modally.

How can this be done?

War es hilfreich?

Lösung

  1. The startup of the MainWindow is defined in App.xaml by default when creating a project in VS:

    <Application ...
                 StartupUri="MainWindow.xaml">
        <Application.Resources>
    
        </Application.Resources>
    </Application>
    
  2. Creating the dialogue in the Loaded event should work, just don't do it in the constructor where it is not yet loaded.

    Loaded="Window_Loaded"
    
    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        new LoginDialogue().ShowDialog();
    }
    

Andere Tipps

One way could be to add a Loaded event handler to your main window and in that display the login window:

this.Loaded += LoadedEventHander;


void LoadedEventHander(object sender, RoutedEventArgs e)
{
    // Show Login.xaml here.
}
  1. In app.xaml the following line defines the startup window, you can change it if you want

    StartupUri="MainWindowView.xaml"
    
  2. If you are following MVVM you can bind a command to the windows loaded event using System.Windows.Interactivity (otherwise simply create an event handler as the others have suggested)

    <i:Interaction.Triggers>
        <i:EventTrigger EventName="Loaded">
            <i:InvokeCommandAction Command="{Binding MyICommand}"/>
        </i:EventTrigger>
    </i:Interaction.Triggers>
    
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top