简体   繁体   English

如何在MVVM中实现模型

[英]How to implement the Model in MVVM

I am currently studying the MVVM pattern. 我目前正在研究MVVM模式。 So thus far I developed a simple demo programm which contains a view and a viewmodel with commands etc. Now I want to implement a Model but I am not quite sure how to do so. 到目前为止,我已经开发了一个简单的演示程序,其中包含一个视图和一个带有命令等的viewmodel。现在我想实现一个Model,但是我不太确定如何实现。 My demo contains a view with a textbox and a button. 我的演示包含一个带有文本框和按钮的视图。 When the Button gets pressed a Command is launched. 当按下按钮时,将启动命令。 The text from the textbox should be written in a textfile with upper cased letters. 来自文本框的文本应使用大写字母写在文本文件中。 This functionality should be part of my model. 此功能应该是我的模型的一部分。 How do i call this functionality from my viewmodel? 如何从我的视图模型中调用此功能? Should the viemodel contain a instance of the model class and call a methode in the command execute? viemodel是否应包含模型类的实例并在执行命令中调用方法? And how does the viewmodel get data from a model? 以及视图模型如何从模型中获取数据?

Thank you very much for your help! 非常感谢您的帮助!

Usually for data storage and retrieval, I create a separate class called repository. 通常,为了进行数据存储和检索,我创建了一个单独的类,称为存储库。 Your view model has an instance of the repository (or better: an interface of it). 您的视图模型具有存储库的一个实例(或更好的是:它的一个接口)。 In the repository class, you can do your file access. 在存储库类中,您可以进行文件访问。

By the way: If your view model just knows the interface of the repository, you could replace it later with a database access, and the view model would not be affected. 顺便说一句:如果您的视图模型只知道存储库的接口,则可以稍后用数据库访问权限替换它,并且视图模型不会受到影响。

The view model can then interact with the repository ie call it methods when the command code in the view model executes. 然后,视图模型可以与存储库进行交互,即在视图模型中的命令代码执行时调用其方法。

You write... 你写...

"The text from the textbox should be written in a textfile with upper cased letters. This functionality should be part of my model." “来自文本框的文本应该用大写字母写在文本文件中。此功能应该是我的模型的一部分。”

The model usually is just data, so a model class does not have functionality but only properties. 该模型通常只是数据,因此模型类不具有功能,而仅具有属性。 Like I said: Do your data access in the view model or in the repository class. 就像我说的:在视图模型或存储库类中进行数据访问。

In case of MVVM it would be good if the properties implement INotifyPropertyChanged , like the properties of your view model. 在MVVM的情况下,如果属性实现INotifyPropertyChanged (如视图模型的属性),那就很好了。

If you just want to write the content of a single text box, then your model would be a class with just one property. 如果您只想编写单个文本框的内容,则您的模型将是仅具有一个属性的类。

Yes. 是。 You could instantiate a model object in the viewmodel and have it save the text in a textfile (Or whatever you want your application to do) 您可以在viewmodel中实例化模型对象,并将其保存在文本文件中(或者您希望应用程序执行的任何操作)

class ViewModelDefault : INotifyPropertyChanged
{
    // Bound to your textbox
    public string TextboxProperty { get; set;}

    // Instantiate modellayer in viewmodel
    private ModelClass _modelClass = new ModelClass();

    // RelayCommand property -> bound to button on viewmodel
    // Will execute method "ExecuteCommand" that contains a call to a method in the ModelClass
    public ICommand ExecuteModelMethod 
    { 
        get {
            RelayCommand relayCommand = new RelayCommand(ExecuteCommand); 
            return relayCommand; 
        } 
    }

    // Method that the RelayCommand will execute.
    private void ExecuteCommand() 
    {
        _modelClass.SaveTextInTextfile(TextboxProperty);
    }

    ...
}

In the code above I've made an example of how this could be done using RelayCommand. 在上面的代码中,我举例说明了如何使用RelayCommand完成此操作。

RelayCommand is a class that makes use of delegates such as Action and Func. RelayCommand是一个使用诸如Action和Func之类的委托的类。 This means you can pass a method into the RelayCommand object and have it execute it. 这意味着您可以将方法传递到RelayCommand对象中,并使其执行它。

What RelayCommand allows you to do is basicly binding a method through delegate to UI control in the view layer. RelayCommand允许您做的基本上是通过委托将方法绑定到视图层中的UI控件。

Read up on Delegates if you wish to study further on that topic. 如果您想进一步研究该主题,请阅读《代表》。 Delegates (C# Programming Guide) 代表(C#编程指南)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM