How to change StartupUri of WPF Application?

爱⌒轻易说出口 提交于 2019-11-28 10:50:11

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.

tofutim

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

Just try in OnStartup() :

StartupUri = new Uri("Forms/CustomerEntry.xaml", UriKind.Relative);

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)

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!