简体   繁体   English

如何在视图模型之间切换视图?

[英]How can I switch views between viewmodels?

My Main window defines the markup for the application, for this specific scenario lets say I have a grid with 2 columns. 我的主窗口定义了应用程序的标记,对于这种特定情况,可以说我有一个包含2列的网格。
First column will have navigation links, and second column will display the different views. 第一列将具有导航链接,第二列将显示不同的视图。
There are 2 views (and 2 viewmodels) defined in mainwindow xaml: 在mainwindow xaml中定义了2个视图(和2个视图模型):

<Window.Resources>       
   <DataTemplate DataType="{x:Type vm:Window1ViewModel}">
       <vw:Window1View/>
   </DataTemplate>
   <DataTemplate DataType="{x:Type vm:Window2ViewModel}">
       <vw:Window2View/>
   </DataTemplate>      
</Window.Resources>

And in second grid column that displays the views i got : 在第二个显示我得到的视图的网格列中:

<ContentControl  Content="{Binding Path=ViewModel}" HorizontalAlignment="Left">                    
</ContentControl>

Where ViewModel is a property that I set accordingly to a view(viewmodel) that i want to display. 其中ViewModel是我要设置为要显示的view(viewmodel)的属性。

Like : 喜欢 :

ViewModel = new Window1ViewModel(); ViewModel =新的Window1ViewModel();
(datacontext of the mainwindowview is set to MainWindowViewModel) (将mainwindowview的datacontext设置为MainWindowViewModel)

So there is no problem to switch between views from the MainWindowViewModel. 因此,在MainWindowViewModel的视图之间切换是没有问题的。

My problem is how to switch within Window1ViewModel into Window1ViewMode2? 我的问题是如何在Window1ViewModel内切换到Window1ViewMode2?

The various ViewModels don't "know" about other ViewModels. 各种ViewModel都不“了解”其他ViewModel。
Only MainWindowViewModel knos about others... 只有MainWindowViewModel知道其他人...

How can I solve this? 我该如何解决? Maybe I should define a custom Event (with parameter), MainWindowViewModel will subscribe and other viewmodels will trigger it and then MainWindowViewModel will switch to the needed view? 也许我应该定义一个自定义事件(带有参数),MainWindowViewModel将订阅,其他视图模型将触发它,然后MainWindowViewModel将切换到所需的视图?

the solution you describe is one possibility. 您描述的解决方案是一种可能性。 One other I can think of is using some kind of Navigation-Service (static class or interface you pass to all your child -Viewmodels) that do this kind of work. 我能想到的另一个方法是使用某种导航服务(静态类或传递给所有视图类的接口-Viewmodels)来完成这种工作。

If your MainWindowViewModel creates all the others I would stick to the interface solution. 如果您的MainWindowViewModel创建了所有其他窗口,我将坚持使用界面解决方案。 You can for example let the MainWindowVM implement such a interface and inject it into all the child-vm on creation. 例如,您可以让MainWindowVM实现这样的接口,并在创建时将其注入所有child-vm中。 This is much the same as your event-approach but instead of the childs-providing and the main having to subscribe you have the main give something ... IMHO the better approach. 这与您的事件处理方法几乎相同,但是除了提供子项和主要订阅服务外,您还可以主要提供一些东西……恕我直言,更好的方法。

Ok, may be I understood your point. 好吧,也许我理解您的意思了。 You want that controller actually be modelview which notifies to mainmodelview about the fact that it have to be swapped with someone else. 您希望该控制器实际上是modelview,它将必须与其他人交换的事实通知mainmodelview。

Considering that we are talking about WPF, create DependecyProperty on mainmodelview , and set it from childview, which in code behind will trigger modelviews swap. 考虑到我们正在谈论WPF,请在mainmodelview上创建DependecyProperty,然后从childview进行设置,这在后面的代码中将触发modelviews交换。

暂无
暂无

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

相关问题 我如何在视图/视图模型所提供的对象之间进行切换,这些对象是提前进行异步初始化的? - How can I switch around Views/ViewModels that are provided objects which are asychronously initialized ahead of time? 如何在不同项目中的视图/视图模型之间导航 - How to navigate between Views/ViewModels in different projects 如何在WPF中的ViewModel之间进行通信以及如何控制视图生命周期 - How to communicate between ViewModels in WPF and how to control Views lifecycle 在多窗口WPF应用程序中如何在ViewModels之间切换? - How to switch between ViewModels in multi-window WPF application? 如何将多个ViewModel嵌套在顶级ViewModel中,并以一种可以分散在整个View中的层次结构进行嵌套? - How do I nest multiple ViewModels in a top level ViewModel, in a sort of hierarchy that can be dispersed throughout my Views? MVVM - 如何从父 ViewModel 引用子 ViewModel(ViewModel 在其视图中创建)? - MVVM - How can I refer to child ViewModel from parent ViewModel (ViewModels are created in their Views)? 我应该如何在ViewModels之间进行通信? - How should I communicate between ViewModels? 视图和视图模型如何在MVC 5中利用基于声明的授权? - How can Views and ViewModels utilize claims-based authorization in MVC 5? 我需要多少个视图,视图模型和模型? - How many views, viewmodels and models do I need? 如何在 Unity 中切换角色? - How can i switch between characters in Unity?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM