MVVM WPF change smoothly one window to another

╄→гoц情女王★ 提交于 2019-12-13 04:51:05

问题


I'm doing an app in WPF with design pattern MVVM. I have there fullsize windows(not only maximized but fullsize).

I need to switch one window to another(open window B by pressing button x in window A and close window A when window B is showns).

I'm closing window A in loaded event in window B but I still can see a desktop for a while before window B is shown.

How to do it without seeing desktop?


回答1:


Instead of switching between windows, you can have one window with a ContentControl. All you'll need to do is change its Content property to switch between views.

I also recommend having a look at the Prism library which has this kind of functionality built in, and specifically its navigation documentation.




回答2:


You can make 2 separate user controls and put the content of each window inside a user control.

inside your main window you define data templates those will decide what is the view to be used based on the current Datacontext

<Window.Resources>
<DataTemplate DataType="{x:Type viewmodels:viewmodel1}" >
     <views:Usercontrol1/>
</DataTemplate>

<DataTemplate DataType="{x:Type viewmodels:viewmodel2}" >
    <views:Usercontrol2/>
</DataTemplate>
</Window.Resources>

Then you create a MainViewModel and define currentviewmodel viewmodel1and viewmodel2 and a command that will change the currentviewmodel and after that set the Datacontext of your MainWindow to the MainViewModel.

in your MainWindow you bind your button to that command, and then you just make the command change the currentviewmodel and inside your MainWindow

<Button Content="change grid data context" Command="{Binding yourCommand}"/>
<Grid DataContext="{Binding currentviewmodel}"/>

you don't worry about which view to use, when the DataContext get set the DataTemplates defines which view to use



来源:https://stackoverflow.com/questions/19751554/mvvm-wpf-change-smoothly-one-window-to-another

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!