简体   繁体   中英

How to implement switching between windows properly in WPF?

I'm creating a game. I have a game menu. Menu's grid have a StackPanel with videofile which is used as a menu background. But I've got a problem. How should I switch to 'for example Settings' window? When I switch like this

var settings = new Settings();
settings.Show();

Videofile is closed. I guess if I put the same stackPanel into the SettingsWindow Grid it will start playing from the beggining.Help :(

A basic way to get you going is to assign multiple layouts to the same column and row in your grid, and then when you hit events when you want to switch the layout that is showing, set the layout.Visibility to Collapsed or Visible appropriately ( https://docs.microsoft.com/en-us/dotnet/api/system.windows.visibility?redirectedfrom=MSDN&view=netframework-4.7.2 )

From your example, you'd want to set your SettingsStackPanel.Visibility=Collapsed followed by GameLayout.Visibility=Visibile.

I would also suggest not using a StackPanel for something that is not changing in size. The Grid layout will have better performance in the long run and better control. This could be a nice habit to get into up front as you're making a game.

As a word of caution, this will still have both layouts still able to update, but it sounds like you're at the very start of your application.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM