有一些空闲时间,想尝试用 WPF 制作游戏。 我想知道,更改窗口 View 的最佳方法是什么? 我制作了一个“主菜单”窗口,其中包含三个按钮……新游戏、继续游戏和退出游戏。 当按下新游戏时,我希望窗口转到下一个“viewstate”以创建播放器等,不希望弹出新窗口。什么是实现它的最佳方式。
最佳答案
如果你想根据 ViewModel 动态切换 View ,使用 DataTemplates 是合适的:
<Window>
<Window.Resources>
<DataTemplate DataType="{x:Type ViewModelA}">
<localControls:ViewAUserControl/>
</DataTemplate>
<DataTemplate DataType="{x:Type ViewModelB}">
<localControls:ViewBUserControl/>
</DataTemplate>
<Window.Resources>
<ContentPresenter Content="{Binding CurrentView}"/>
</Window>
如果 Window.DataContext
是 ViewModelA
的一个实例,那么 ViewA
将被显示并
Window.DataContext
是ViewModelB的一个实例,那么ViewB就会显示出来。
我见过和读过的最好的例子是由 Rachel Lim 制作的. See the example .
关于c# - 如何更改 WPF 中的 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33652707/