c# - 当我的 Model 和 ViewModel 完全相同时,我是否需要使用 MVVM? WP 8.1

标签 c# mvvm windows-phone-8.1

我正在开发 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)到下一个问题?我是否实现了 INotifyPropertyChangedQuestion 上类并为问题的每个属性触发它?还是只有一个属性就足够了?另外,如果我这样做,就没有 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/

相关文章:

wpf - 如何通过 XAML 设置 WPF 超链接文本

c# - Windows Phone 8.1 在代码隐藏中手动更改资源文件

windows-phone-8 - Windows Phone 8.1 Silverlight 应用程序中 Package.appxmanifest.xml 文件的用途是什么?

c# - Visual C# 2010 Express : Opening a design and designer tab for a new form in the application

WPF 数据网格绑定(bind)工具提示在表格内容绑定(bind)刷新时闪烁

c# - Angular 应用程序无法在 dotnet core/IIS 下启动

c# - 处理 MVVM 中的周期性线程

c# - 创建包提交到 Windows Phone 8.1 商店时出错

c# - 无法加载项目文件,根级别的数据无效,项目中没有 XML

c# - 使用对象列表填充 UserControl Gridview