How to change StartupUri of WPF Application?

前端 未结 4 457
说谎
说谎 2020-12-31 03:26

I am trying to modify App.cs and load the WPF XAML files from code behind but its not working as it should.

No matter whatever I try to set as StartupUri it doesnt s

相关标签:
4条回答
  • 2020-12-31 03:29

    Just try in OnStartup() :

    StartupUri = new Uri("Forms/CustomerEntry.xaml", UriKind.Relative);
    
    0 讨论(0)
  • 2020-12-31 03:46

    Do you still have a StartupUri specified in the XAML? If so, remove it and see if that helps.MSDN Source

    If not, you may need to approach this differently: have your Dialog as your startup, then from that point open another Window based on the selected value.

    0 讨论(0)
  • 2020-12-31 03:53

    instead of overriding the OnStartup() method, hook into the event instead.

    in the XAML

    <Application x:Class="SOTestWPF.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>
    

    in the code behind:

        private void Application_Startup(object sender, StartupEventArgs e)
        {
            var rnd = new Random();
    
            if (rnd.NextDouble() > 0.5)
                StartupUri = new Uri("/SOTestWPF;component/Window1.xaml", UriKind.Relative);
            else
                StartupUri = new Uri("/SOTestWPF;component/Window2.xaml", UriKind.Relative);
    
        }
    

    This is only my test case and I have verified that it performs correctly (randomly :D)

    0 讨论(0)
  • 2020-12-31 03:54

    Akash, I ran into this exactly issue trying to implement a LoginDialog just like yours. The dialog does not have a bug, but rather the behavior is by design.

    Not a bug. The default ShutdownMode of Application is OnLastWindowClosed, so as soon as the first window is closed your application will start shutting down! Change to OnExplicitShutdown and it will work, but you'll have to manage the shutdown.

    See this previous StackOverflow question: WPF ShowDialog returns null immediately on second call

    0 讨论(0)
提交回复
热议问题