Question

I have an WPF application, when at first start displays window to select language. So, in App.xaml:

<Application x:Class="MyApp.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="WindowLanguage.xaml">

in WindowLanguage:

public partial class WindowLanguage : Window
{
    bool mainWindowOpened = false;
    public WindowLanguage()
    {
        if (!Settings.Instance.firstStart)
        {
            MainWindow mainWindow = new MainWindow();
            mainWindow.Show();
            Close();
        }

It works, but unnecessary Window is init.

I think about following way: App.xaml.cs

protected override void OnStartup(StartupEventArgs e)
{
    base.OnStartup(e);
    if (!Settings.Instance.firstStart)
        StartupUri = new Uri("/MyApp;component/MainWindow.xaml", UriKind.Relative);

}

This second way with changing StartupUri is better or not? Which way is the best for my situation (open WindowLanguage during first start of app)?

Was it helpful?

Solution

Setting the startupUri is always better than recreating window all over again.

Also there are other options for opening window based on some conditions like having age old console Main method for opening window. Few more options can be found here.

protected override void OnStartup(StartupEventArgs e)
{
    base.OnStartup(e);
    if (!Settings.Instance.firstStart)
    {
        MainWindow mainWindow = new MainWindow();
        mainWindow.Show();
    }
    else
    {
       WindowLanguage windowLanguage = new WindowLanguage();
       windowLanguage.Show();
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top