Code Monkey home page Code Monkey logo

mvvm's Introduction

基于 INotifyPropertyChanged 的 MVVM 简单实现

使用

Model 层

Model 层由于 ViewModel 层用了 INotifyPropertyChanged 的新的触发方法 Model 层只能用字段,使用原触发方法可以用属性。

public class ModelTemplate
{
    public int id;
    public string title;
    public DateTime releaseDate;
    public string genre;
    public decimal price;
}

ViewModel 层

ViewModel 需要继承 ViewModelBase 类。属性:

public int ItemScore
{
    get => _scoreItem.score;
    set => SetField (ref _scoreItem.score, value);// 新触发方法
}

View 层

View 层需要继承 ViewBase<T> 类。 需要使用 InitViewModel () 方法初始化 ViewModel。 需要使用 BindText () 方法绑定 UI。 BindText () 参数分别为 UI 控件、ViewModel 属性名、ViewModel 属性的 get 委托。

InitViewModel (_scoreItemViewModel);
BindText (nickName,nameof (_scoreItemViewModel.ItemNick),()=>_scoreItemViewModel.ItemNick);

参考

数据绑定和 MVVM

Model-View-ViewModel (MVVM)

Input Field

INotifyPropertyChanged

PropertyChangedEventHandler 委托

GPTchat: 属性的引用

重构

v0.0.00

  • 新建项目
  • 初始化场景

v0.0.01

  • 普通 UI 脚本

v0.0.02

  • 分离 Model
  • 分离 ViewModel

v0.0.03

  • 抽象 ViewModel
  • 抽象 View

mvvm's People

Contributors

kinoiralink avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.