Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing argument to StartupUri in WPF

Tags:

c#

wpf

I've got a simple WPF application with the usual static Main() (entry point to application). Main will do some initialisation stuff before the UI is displayed. It will then creating and run the start-up Window. However, what I need to do is pass a custom object from main to start-up Window but I'm not sure how to do it.

My main class containing Main() looks like this:

class App : Application
{
    [STAThread()]
    static void Main()
    {
        MyObject obj;
        // Some processing stuff here.

        new App(obj);
    }

    public App(MyObject obj)
    {
        StartupUri = new System.Uri("MainWindow.xaml", UriKind.Relative);
        Run();
    }
}

Obviously, MyObject is my custom object that I would like to have access to in my start-up Window. How can I do this?

TIA

like image 451
millie Avatar asked Jan 26 '12 18:01

millie


1 Answers

Add a parameter of type MyObjectto your MainWindow constructor (or a property if your prefer), then create your window manually. Simply use the overload of Run that takes a Window parameter rather than using a startup URI to show this window as the main window.

Run(new MainWindow(obj));
like image 128
Julien Lebosquain Avatar answered Oct 10 '22 11:10

Julien Lebosquain