[UWP] Make Model和View在MVVM中是独立的 [英] [UWP]Make Model and View are independent in MVVM
问题描述
大家好,
我尝试应用MVVM模式来制作我的培训项目。我知道模型和视图必须在MVVM模式中是独立的。但在这种情况下我不知道如何单独保留它们:
I try to apply MVVM pattern to make my training project. I know model and view must be independent in MVVM pattern. But I don't know how to keep them separately in this case:
- 模型定义数据类型
- Model defines a data type
- 使用gridview控件查看,每个此控件的项目已格式化 显示数据类型的实例
- View using gridview control, each item of this control is formatted to show an instance of data type
在示例中:https://github.com/Windows-Readiness/AbsoluteBeginnersWin10/tree/master/UWP-077/UWP-077/HeroExplorer/ HeroExplorer
In the sample: https://github.com/Windows-Readiness/AbsoluteBeginnersWin10/tree/master/UWP-077/UWP-077/HeroExplorer/HeroExplorer
我看到该视图取决于模型。它是否违反了MVVM模式?如何解决?
I saw that view depend on model. Does it violate MVVM pattern?. How to resolve ?
//Model define class
public class Character
{
public int id { get; set; }
public string name { get; set; }
public string description { get; set; }
public string modified { get; set; }
public Thumbnail thumbnail { get; set; }
public string resourceURI { get; set; }
public Comics comics { get; set; }
public SeriesList series { get; set; }
public Stories stories { get; set; }
public Events events { get; set; }
public List<Url> urls { get; set; }
}
//View using this class type in ListView datatemplate
<Page
//.....
xmlns:data="using:HeroExplorer.Models"
//....
mc:Ignorable="d">
//......
<ListView Name="MasterListView"
IsItemClickEnabled="True"
ItemClick="MasterListView_ItemClick"
ItemsSource="{x:Bind MarvelCharacters}">
<ListView.ItemTemplate>
<DataTemplate x:DataType="data:Character">
<StackPanel Orientation="Horizontal" Margin="10,5,0,5">
<Ellipse Width="45" Height="45">
<Ellipse.Fill>
<ImageBrush ImageSource="{x:Bind thumbnail.small}" />
</Ellipse.Fill>
</Ellipse>
<TextBlock Text="{x:Bind name}"
VerticalAlignment="Center"
Margin="10,0,0,0"
FontSize="18" />
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
//......
推荐答案
99%例如,您无法将视图与模型完全隔离。事实上,视图和模型将始终紧密耦合在一起,但是编写代码的方式将支持一定程度的分离。
in 99% of cases, you can't completely isolate the view from the model. In fact, the view and the model will always be tightly coupled together, but the way you write the code will support some degree of separation.
随着您的用例变得越来越复杂,你会发现将视图与模型分开并不总是可行的,特别是在开始处理线程编组时,这将迫使模型引用"视图"。成员:
调度员。
As your use cases get more and more complex, you will see that separating the view from the model is not always possible, especially when heaving to deal with thread marshaling, which will force the model to have a reference to a "view" member: the dispatcher.
在特定情况下,MVVM模式没有中断。该模型将始终命令视图的外观。这是MVVM的重点。该模型有几种影响视图外观的方式,如数据模板选择器,转换器,
状态模型等。
In your specific case, there is no breaking of MVVM pattern. The model will always command how the view looks like. This is the whole point of MVVM. The model has several ways of influencing how the view looks like, like data template selectors, converters, state models, etc.
这篇关于[UWP] Make Model和View在MVVM中是独立的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!