Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7781791
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T19:20:26+00:00 2026-06-01T19:20:26+00:00

My mainWindow needs to subscribe to some events from an object. The object is

  • 0

My mainWindow needs to subscribe to some events from an object. The object is initialized before the MainWindow is created. I would like to pass this object to the mainWindow via its constructor.

However I can’t figure out from where the MainWindow constructor is called. Alternatively I tried to pass the object via a member function of the MainWindow, but the app.MainWindow is null before app.Run() is called. After app.Run() is called the code doesn’t return until the program terminates.

Another posibility would be storing the object in a static class and have the MainWindow access that, but this seems unnecessarily complicated.

I realize I can just create the object in the MainWindow constructor, but that would mean having to put a lot of other code there as well, pretty much the entire Main function.

How can I pass this object to my MainWindow? Or is the MainWindow constructor intended to function as the ‘Main’ for the entire program?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T19:20:28+00:00Added an answer on June 1, 2026 at 7:20 pm

    You could do it like this.

    First go into App.xaml and remove this line StartupUri="MainWindow.xaml" to prevent WPF from automatically showing the MainWindow.

    Next right click on App.xaml and choose View Code to open up App.xaml.cs. Inside this file we need to to override the OnStartup event.

    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);
    }
    

    Inside OnStartup we can then instantiate our MainWindow and show it.

    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);
    
        MainWindow mw = new MainWindow();
        mw.Show();
    }
    

    And now we can use this to load an alternative Constructor that we can use to pass on more information.

    App.xaml.cs

    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);
    
        MainWindow mw = new MainWindow(5);
        mw.Show();
    }
    

    MainWindow.xaml.cs

    public MainWindow()
    {
        InitializeComponent();
    }
    
    public MainWindow(int number) : base()
    {
    
    }
    

    I prefer to chain my constructors, but it’s of course not a requirement by any means.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

public MainWindow() { CommandManager.AddExecutedHandler(this, ExecuteHandler); } void ExecuteHandler(object sender, ExecutedRoutedEventArgs e) { } Error
I would like to get the XAML source of a WPF Window (MainWindow). Clicking
I have some Events I created on my own and was wondering on how
I need some event like the SizeChanged on MainWindow that is activated when the
void MainWindow::on_actionAlways_on_Top_triggered(bool checked) { Qt::WindowFlags flags = this->windowFlags(); if (checked) { this->setWindowFlags(flags | Qt::CustomizeWindowHint
I have a custom class that I would like to bind a WPF TreeView
I have a Qt Designer Class called MainWindow which is a QMainWindow. This class
Simple synopsis: I have a program which needs authentication from user to get access
I have an Air application with a main window. I would like to have
I have a main window with some widgets on it, each needs its own

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.