Passing StartupUri argument to WPF

I have a simple WPF application with the usual static Main () (entry point to the application). Main will perform some initialization operations until the user interface is displayed. Then it will create and launch a launch window. However, what I need to do is pass the user object from the main to the initial window, but I'm not sure how to do it.

My main class containing Main () is as follows:

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 access in my initial window. How can i do this?

TIA

+4
source share
1 answer

Add a parameter of type MyObject to your MainWindow constructor (or property, if you prefer), and then create your window manually. Just use the Run overload, which accepts the Window parameter, rather than using the launch URI, to display this window as the main window.

 Run(new MainWindow(obj)); 
+3
source

Source: https://habr.com/ru/post/1393225/


All Articles