简体   繁体   中英

WPF MainWindow Override OnStartup

I can't get my MainWindow to show without using StartupUri. OnStartup doesn't trigger.

I removed StartupUri from App.xaml

<Application x:Class="SCon"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
</Application>

added the following in the code behind

    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);
        SCon.MainWindow.Instance.Show();
    }

MainWindow is a singleton

    private static volatile MainWindow instance = null;
    private static object lockThis = new object();
    public static MainWindow Instance
    {
        get
        {
            if (instance == null)
            {
                lock (lockThis)
                {
                    if (instance == null)
                    {
                        instance = new MainWindow();
                    }
                }
            }
            return instance;
        }
    }

Idk what you mean or where you stuck at. I would check Build Actions of App.xaml then I would take a look to this site.

http://www.erikojebo.se/Code/Details/202

That guy explains is pretty well :)

Also your question is sort of a duplicate. Check this out: How to change StartupUri of WPF Application?

You need to put the following line in the XAML file:

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

That's why it didn't trigger the OnStartup method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM