c# - Initializing a WPF-window with an optional parameter -



c# - Initializing a WPF-window with an optional parameter -

i have 2 windows. main window , autoimport.the main goal switch between windows, while keeping 1 window active , not closing it. 1 show while other hidden. i'm able using 1 window parameter in other. problem have run when startup window 1 parameter. i'm guessing needs optional? i'm not sure how window.

when projects starts, main window comes up. code is

public partial class mainwindow : window { private autoimport auto; public mainwindow(autoimport parent) { initializecomponent(); auto = parent; } public void btnautoimport_click(object sender, routedeventargs e) { this.hide(); auto.show(); } }

if click button, main window should hide , other window should appear. code is

public autoimport() { initializecomponent(); } private void button_click(object sender, routedeventargs e) { this.hide(); mainwindow main = new mainwindow(this); main.show(); }

changes made autoimport , when button clicked on form, autoimport hide , new mainwindow come up. if button on main window clicked again, autoimport come changes made earlier. have no problems doing when switch code on forms.

to asking add together constructor mainwindow:

public partial class mainwindow : window { private autoimport auto; public mainwindow() { initializecomponent(); } public mainwindow(autoimport parent) { initializecomponent(); auto = parent; } public void btnautoimport_click(object sender, routedeventargs e) { this.hide(); if (auto == null) { auto = new autoimport(); } auto.show(); } }

although think might create more sense maintain re-create of instance of mainwindow in autoimport since hiding it. when create instance of mainwindow, create instance of autoimport setting mainwindow property/field of autoimport. when click button, windows swapped. way don't have create new mainwindow every time. this:

public partial class mainwindow : window { private autoimport auto; public mainwindow() { initializecomponent(); } public mainwindow_loaded(object sender, eventargs e) { auto = new autoimport(this); } public void btnautoimport_click(object sender, routedeventargs e) { this.hide(); auto.show(); } } public class autoimport { private window linkedwindow { get; set; } public autoimport(window parent) { initializecomponent(); linkedwindow = parent; } private void button_click(object sender, routedeventargs e) { this.hide(); linkedwindow.show(); } }

c# wpf

Comments

Popular posts from this blog

php - Edges appear in image after resizing -

ios8 - iOS custom keyboard - preserve state between appearances -

Delphi change the assembly code of a running process -