我正在开发 Windows Phone 8.1(非 SL)应用程序。这是一款针对 child 的水果和蔬菜问答游戏。这是我的整个模型:
namespace FruityLogic.Models
{
public class Quiz
{
public int lives { get; set; }
public Question Question { get; set; }
public List<Feg> Feges { get; set; } // Feg meaning Fruit and Veg ;)
public List<int> IdsOfDoneFeges { get; set; }
public Quiz()
{
LoadData();
lives = 3;
IdsOfDoneFeges = new List<int>();
Question = new Question()
{
Feg = Feges[0],
OptionFeges = new List<Feg>()
{
Feges[1],
Feges[2],
Feges[0]
}
};
}
public void LoadData()
{
Feges = new List<Feg>()
{
new Feg() { Id = 0, Name = "Banana", Image = "../assets/banana.jpg"},
new Feg() { Id = 1, Name = "Orange", Image = "../assets/orange.jpg"},
new Feg() { Id = 2, Name = "Apple", Image = "../assets/apple.jpg"},
new Feg() { Id = 3, Name = "Cherry", Image = "../assets/cherry.jpg"},
new Feg() { Id = 4, Name = "watermelon", Image = "../assets/watermelon.jpg"},
new Feg() { Id = 5, Name = "Lettuce", Image = "../assets/lettuce.jpg"},
};
}
}
}
测验应在屏幕上显示水果或蔬菜的图片,用户将为显示的水果/蔬菜选择正确的名称。
我在 MVVM 方面苦苦挣扎。我读了this tutorial ,但它并不真正适用于我,或者至少我不能让它相关!
据我所知,我的模型和 ViewModel 完全相同。我需要创建一个 ViewModel 吗?
另外,我如何通知我的 View ,一个问题已经改变,以便它绑定(bind)到下一个问题?我是否实现了 INotifyPropertyChanged
在 Question
上类并为问题的每个属性触发它?还是只有一个属性就足够了?另外,如果我这样做,就没有 ObservableObject<>
围绕我的问题 Quiz
类,测验如何知道问题已更改?
如您所知,我对所有这些感到困惑,非常希望得到一些澄清。
最佳答案
My Model and ViewModel are exactly the same as far as I can see. Do I bother creating a ViewModel?
是的。您需要一个 ViewModel 来将 View 数据绑定(bind)到。问题是你是否需要一个单独的模型。这取决于你用它做什么,比如存储在 Db 或文件中。但单独的模型通常是个好主意。
并且您的模型属性都没有实现 INotifyPropertyChanged,这是需要解决的问题,例如通过使用 INPC 在 VM 中复制它们。
Also, how can I inform my View, that a Question has changed, so that it binds to the next question?
通过添加 SelectedQuestion 属性。这是 VM 的任务。 “选定”对于模型通常不是一个有意义的概念。
总而言之,ViewModel 应该执行“Presentation Logic”,而 Model 应该执行“Bussines Logic”。
ViewModel 可以非常简单,只需将整个 Model 类作为单个属性转发即可。然后添加 SelectedSomething 等
我一直很喜欢this picture ,但其他人觉得它太复杂了。
关于c# - 当我的 Model 和 ViewModel 完全相同时,我是否需要使用 MVVM? WP 8.1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31158438/