wpf 一个 ViewModel,多个视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18788671/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
One ViewModel, multiple views
提问by Walkingsteak
I am having a hard time getting multiple views to work against 1 viewmodel. I have read Naming Convention for Multi-View Supportwithout getting much out of it, and have tried countless things in the process.
我很难让多个视图针对 1 个视图模型工作。我已经阅读了多视图支持的命名约定,但并没有从中得到太多,并且在此过程中尝试了无数的事情。
Just to take a simple example. Say I have a ViewModel for People residing in ShellViewModel, which basically contains a list of Person-objects. I want to display them in two different ways in my application.
举个简单的例子。假设我有一个用于居住在 ShellViewModel 中的人的 ViewModel,它基本上包含一个 Person-objects 列表。我想在我的应用程序中以两种不同的方式显示它们。


What is the correct way to name the Views in this case, and how do I display both views in ShellView?
在这种情况下命名视图的正确方法是什么,如何在 ShellView 中显示两个视图?
采纳答案by Chris
Anders is correct, there are a number of default conventions for Caliburn.Micro, one of them will locate and display <RootNS>.Views.[<ChildNS>].<ViewTypeName>for <RootNS>.ViewModels.[<ChildNS>].<ViewModelTypeName>.
安德斯是对的, 有许多默认约定Caliburn.Micro,其中之一将定位并显示<RootNS>.Views.[<ChildNS>].<ViewTypeName>为<RootNS>.ViewModels.[<ChildNS>].<ViewModelTypeName>。
In your case, for a single View(assuming the classes reside in namespaces derived from the folders):
在您的情况下,对于单个View(假设类驻留在从文件夹派生的命名空间中):
<RootNS>.Views.PeopleViewwould by located and displayed for <RootNS>.ViewModels.PeopleViewModel.
<RootNS>.Views.PeopleView将被定位并显示为<RootNS>.ViewModels.PeopleViewModel。
For multiple views over the same viewmodel, the convention is that views of format <EntityName>.<Context>are displayed for viewmodels of format <EntityName>[<ViewSuffix>]ViewModel:
对于同一视图模型上的多个视图,约定是为格式<EntityName>.<Context>视图模型显示格式视图<EntityName>[<ViewSuffix>]ViewModel:
From your example, you could create a new folder named People, and inside it, create your views named Gridand List.
在您的示例中,您可以创建一个名为 的新文件夹People,并在其中创建名为Grid和的视图List。
Your namespaces become <RootNS>.Views.People.Gridand <RootNS>.Views.People.Listand, should then be located and displayed for <RootNS>.ViewModels.PeopleViewModel.
你的命名空间变成<RootNS>.Views.People.Gridand <RootNS>.Views.People.List,然后应该被定位并显示为<RootNS>.ViewModels.PeopleViewModel。
You typically then display the Viewsin something like a ContentControl, choosing the Viewyou want to display by setting the cal:View.Contextproperty. You'll either hard code the name, if the context isn't going to change in that particular control, or bind to a property which describes what state the ViewModelshould be displayed as.
然后,您通常会Views在类似 aContentControl的View内容中显示 ,通过设置cal:View.Context属性来选择要显示的。如果上下文不会在该特定控件中更改,您要么对名称进行硬编码,要么绑定到描述ViewModel应显示为何种状态的属性。
e.g.
例如
<ContentControl cal:View.Model="{Binding Path=ActiveItem}"
cal:View.Context="List" />
See the Multiple Views over the Same ViewModelsection.
请参阅同一 ViewModel 上的多个视图部分。
回答by Anders Gustafsson
As far as I can tell from the documentation you are referring to, you should notuse Viewin your view name. Name your view classes People.Gridand People.Listinstead.
至于我可以从你指的是文档告诉,你应该不使用查看在您的视图名称。将您的视图类命名为 People.Grid和People.List。

