Tuesday, December 23, 2014

WPF - Set Startup Window Based on Some Condition

Here I will like to explain you that how to set startup form in WPF window application based on non-conditional and conditional.

choose startup window without any condition

look into App.xaml
Change default xaml startuoUri with your file : StartupUri="MainWindow.xaml" 

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

choose startup window with some condition

look into App.xaml
remove StartupUri="MainWindow.xaml" 
add Startup="Application_Startup" new event Handler

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

From code behind App.xaml.cs create Application_Startup event as follow:
private void Application_Startup(object sender, StartupEventArgs e)
    {
        //add some startup logic
        var identity = AuthService.CheckLogin();
        if (identity == null)
        {
            //if identity is null then show login window
            //LoginWindow is your win form(Xaml File)
            LoginWindow login = new LoginWindow();
            login.Show();
        }
        else
        {
            //if identity is not null then show main window
            MainWindow mainView = new MainWindow();
            mainView.Show();
        }
    }


No comments:

Post a Comment