简体   繁体   English

如何在 WPF 中正确实现窗口之间的切换?

[英]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.菜单的网格有一个带有视频文件的 StackPanel,用作菜单背景。 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 :(我想如果我将相同的 stackPanel 放入 SettingsWindow Grid 中,它将从 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 )让你开始的一个基本方法是将多个布局分配给网格中的同一列和行,然后当你想要切换显示的布局时点击事件时,将 layout.Visibility 适当地设置为 Collapsed 或 Visible ( 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.从您的示例中,您希望设置 SettingsStackPanel.Visibility=Collapsed 后跟 GameLayout.Visibility=Visibile。

I would also suggest not using a StackPanel for something that is not changing in size.我还建议不要将 StackPanel 用于大小不变的东西。 The Grid layout will have better performance in the long run and better control. Grid 布局从长远来看会有更好的性能和更好的控制。 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.需要注意的是,这两个布局仍然可以更新,但听起来您还处于应用程序的最开始阶段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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