C
C#2y ago
Ar1el

WPFUserControl help

hi, so I create a hangman game via wpf (c# and xaml based) and I have two game screens: menu and the actual game. my menu is set to be my MainWindow and my game is set to be Page1. I have a button in my MainWindow that sends me to Page 1 and works just fine, but for some reason doing the same thing from page1 to MainWindow crashes the program with a weird exeption. (the two pics with no exeptions are from Mainwindow -> Page1, the expetions are form Pag1 -> MainWindow)
9 Replies
Ar1el
Ar1elOP2y ago
trying a different approach, i don't even know anymore.
Ar1el
Ar1elOP2y ago
I don't need a hero, i need the whole s.h.i.e.l.d Helicarrier
Klarth
Klarth2y ago
So a <Window> must be a root element. It can't contain other <Window>s.
Klarth
Klarth2y ago
When I do navigation, I typically do one <Window> which has a <ContentControl> and then swap in/out a child control made as a <UserControl>. WPF also has a Page/Frame system that I don't use. There's also Window -> Window navigation, but it's more complicated and I don't recommend it for most cases anyways.
Ar1el
Ar1elOP2y ago
can you pls explain more about contentcontrol? i've set my game to now be in usercontrol and not in page and i've set a button on my MainWindow to transfer me over to my UserControl I still have the same problem while going from my UserControl to my MainWIndow tho HangMan.UserControl1 game = new HangMan.UserControl1(); this.Content = game; again, by adding those i'm able to go from MainWindow to UserControl1 via button, but not vice versa
Klarth
Klarth2y ago
Can you explain exactly what you mean by that? Compiler error? Runtime exception? Something else?
Ar1el
Ar1elOP2y ago
nvm, i finally fixed it created 2 usercontrols with one of em' being my menu and one of being my game then I changed the MainWindow code so now all it does is set the main window content to be usercontrol2 which is my menu and going between usercontrols is now done ty
Klarth
Klarth2y ago
Yeah, that's how you do it. Your Window should be basically empty besides handling some navigation stuff so it can swap in/out content.
Ar1el
Ar1elOP2y ago
yup, tysm, i've been at it for oh so long
Want results from more Discord servers?
Add your server