c# - How I re-open WPF window in the same pragrom/application? -


Everyone,

I would like an application that controls the window open and closed multi-time can do. For example, press 1, open a window; Press 2, close the window.

And I got my code, which can only do this once.

Can someone be good to help me correct? Thanks a lot. Public static zero main (string [] arg) {var appthread = new thread (new threadstart ((=) = & gt; {app = new testWpf.App (); app .ShutdownMode = ShutdownMode.OnExplicitShutdown; app.Run ();})); Appthread.SetApartmentState (ApartmentState.STA); Appthread.Start (); Whereas (true) {var key = Console.ReadKey () Key; // Press 1 to open (key == ConsoleKey.D1) {DispatchToApp ((=) New window (.) Show ()); } // Press 2 to exit (key == ConsoleKey.D2) {DispatchToApp ((=)> app. Shutdown ()); }}} Fixed Zero DispatchToApp (action action) {app.Dispatcher.Invoke (action); }

When you close your app by calling the app. Shut down () You will need to open a windows of it to run it again.

You should save the context of your window and close it instead of closing the entire app. To do this, you need a remittance method that is capable of returning the context of the window:

  Fixed zero main (string [] arg) {var appthread = new thread (New Thread Start () = & Gt; {app = new testWpf.App (); app. Shutdownmod = shutdownmoda.on expedition shutdown; app.ron ();})); Appthread.SetApartmentState (ApartmentState.STA); Appthread.Start (); Window win = new window (); Whereas (true) {var key = Console.ReadKey () Key; // Press 1 to open (key == ConsoleKey.D1) {win = DispatchToApp & lt; Window & gt; (() => {Var myWindow = new window (); myWindow.Show (); myWindow;}); } // Press 2 to exit (key == ConsoleKey.D2) {DispatchToApp ((=) = win.Close ()); }}} Static traction dispatch tap app & lt; Tretton & gt; (Funk & lt; Traton & gt; Action) {Return app Dispatch. Invoke & lieutenant; Treasurer & gt; (Action); } Fixed Zero Dispataptation (Action Action) {app.Dispatcher.Invoke (action); }    

Comments

Popular posts from this blog

c - Mpirun hangs when mpi send and recieve is put in a loop -

python - Apply coupon to a customer's subscription based on non-stripe related actions on the site -

java - Unable to get JDBC connection in Spring application to MySQL -