❔ Reloading MainWindow after some changes
Hello guys,
im currently working on a project to improve my C#/programming skills.
I have following problem:
When my application starts, it checks a filepath and if the filepath is valid, a combobox gets filled with data.
If it isnt valid, I can type a drive Letter in a TextBox (the filepath after the driveletter is usually the same).
After I typed in the new drive letter I click a button which replaces the drive letter in the existing filepath string.
After that I want my MainWindow to be "reloaded" as if I initially opened it with the right filepath.
The filepath has a setter and a getter, thats how I work with that variable.
My code is kinda ugly, so I dont wanna get bashed because of it and hope my description helps for now?
Thank you!
9 Replies
to be frank, I fail to see what the issue is
you wrote some code
you are in complete control of how and when it runs
you have described the exact scenario of when it should run
A) on startup of the application, after the window loads
B) after the user clicks a button to supply a new filepath to load from
But how do I get my Window to "reload"? in my setter I put in the InitializeComponent(); but doesnt seem to do something?
you take the code that reloads the window and call it again
that does NOT consist of InitializeComponent()
InitializeComponent() is essentially your XAML
it is generated code, not the code you wrote to "load" the window
but how do I load my "startup" code? I know in WinForms you could call the Form_Load, but in WPF? do I call the MainWindow() again?
and is it generally bad design to just put non-OOP code directly under InitializeComponent();?
yes and no
you don't "load" code
you "execute" code
if there is code you want to execute more than once, you put it somewhere that allows you to execute it more than once
so basically in its own class?
or method
if you've put it in the MainWindow constructor, underneath InitializeComponent(), then you have put it someplace that cannot be executed more than once
(per window, anyway)
no
yes
okay, then Im gonna try that, thanks!
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.