0

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)?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Oleg Sh
  • 8,496
  • 17
  • 89
  • 159

1 Answers1

1

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();
    }
}
Community
  • 1
  • 1
Rohit Vats
  • 79,502
  • 12
  • 161
  • 185
  • Rohit, since this answer affects the order in which windows are created, would it have a knock on effect to which ShutdownMode should be selected? – Gayot Fow Jul 20 '13 at 14:25
  • 2
    No, order of window is not changed. Its just which window needs to be shown on app launch. So, no change required in shutDownMode. – Rohit Vats Jul 20 '13 at 14:29
  • 1
    thank you! One note - to use your code it's necessary to remove StartupUri, if you don't do it - open MainWindow twice – Oleg Sh Jul 20 '13 at 15:22
  • Yeah, forgot to mention that. – Rohit Vats Jul 20 '13 at 16:18