博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
MVVM: 通过 x:Bind 实现 MVVM(不用 Command)
阅读量:6259 次
发布时间:2019-06-22

本文共 6395 字,大约阅读时间需要 21 分钟。

背水一战 Windows 10 之 MVVM(Model-View-ViewModel)

  • 通过 x:Bind 实现 MVVM(不用 Command)

示例
1、Model
MVVM/Model/Product.cs

/* * Model 层的实体类,如果需要通知则需要实现 INotifyPropertyChanged 接口 */using System.ComponentModel;namespace Windows10.MVVM.Model{    public class Product : INotifyPropertyChanged    {        public Product()        {            ProductId = 0;            Name = "";            Category = "";        }        private int _productId;        public int ProductId        {            get { return _productId; }            set            {                _productId = value;                RaisePropertyChanged(nameof(ProductId));            }        }        private string _name;        public string Name        {            get { return _name; }            set            {                _name = value;                RaisePropertyChanged(nameof(Name));            }        }        private string _category;        public string Category        {            get { return _category; }            set            {                _category = value;                RaisePropertyChanged(nameof(Category));            }        }                public event PropertyChangedEventHandler PropertyChanged;        protected void RaisePropertyChanged(string name)        {            if (PropertyChanged != null)            {                PropertyChanged(this, new PropertyChangedEventArgs(name));            }        }    }}

MVVM/Model/ProductDatabase.cs

/* * Model 层的数据持久化操作(本地或远程) *  * 本例只是一个演示 */using System;using System.Collections.Generic;using System.Linq;namespace Windows10.MVVM.Model{    public class ProductDatabase    {        private List
_products = null; public List
GetProducts() { if (_products == null) { Random random = new Random(); _products = new List
(); for (int i = 0; i < 100; i++) { _products.Add ( new Product { ProductId = i, Name = "Name" + i.ToString().PadLeft(4, '0'), Category = "Category" + (char)random.Next(65, 91) } ); } } return _products; } public List
GetProducts(string name, string category) { return GetProducts().Where(p => p.Name.Contains(name) && p.Category.Contains(category)).ToList(); } public void Update(Product product) { var oldProduct = _products.Single(p => p.ProductId == product.ProductId); oldProduct = product; } public Product Add(string name, string category) { Product product = new Product(); product.ProductId = _products.Max(p => p.ProductId) + 1; product.Name = name; product.Category = category; _products.Insert(0, product); return product; } public void Delete(Product product) { _products.Remove(product); } }}

2、ViewModel
MVVM/ViewModel2/ProductViewModel.cs

/* * ViewModel 层 */using System.Collections.ObjectModel;using System.ComponentModel;using Windows.UI.Xaml;using Windows10.MVVM.Model;namespace Windows10.MVVM.ViewModel2{    public class ProductViewModel : INotifyPropertyChanged    {        // 用于提供 Products 数据        private ObservableCollection
_products; public ObservableCollection
Products { get { return _products; } set { _products = value; RaisePropertyChanged(nameof(Products)); } } // 用于“添加”和“查询”的 Product 对象 private Product _product; public Product Product { get { return _product; } set { _product = value; RaisePropertyChanged(nameof(Product)); } } // 数据库对象 private ProductDatabase _context = null; public ProductViewModel() { _context = new ProductDatabase(); Product = new Product(); Products = new ObservableCollection
(_context.GetProducts()); } public void GetProducts(object sender, RoutedEventArgs e) { // 从 Model 层获取数据 Products = new ObservableCollection
(_context.GetProducts(Product.Name, Product.Category)); } public void AddProduct(object sender, RoutedEventArgs e) { // 在 Model 层添加一条数据 Product newProduct = _context.Add(Product.Name, Product.Category); // 更新 ViewModel 层数据 Products.Insert(0, newProduct); } public void UpdateProduct(object sender, RoutedEventArgs e) { Product product = ((FrameworkElement)sender).Tag as Product; // 更新 ViewModel 层数据 product.Name = product.Name + "U"; product.Category = product.Category + "U"; // 更新 Model 层数据 _context.Update(product); } public void DeleteProduct(object sender, RoutedEventArgs e) { Product product = ((FrameworkElement)sender).Tag as Product; // 更新 Model 层数据 _context.Delete(product); // 更新 ViewModel 层数据 Products.Remove(product); } public event PropertyChangedEventHandler PropertyChanged; protected void RaisePropertyChanged(string name) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(name)); } } }}

3、View
MVVM/View/Demo2.xaml

转载于:https://www.cnblogs.com/ansen312/p/5913405.html

你可能感兴趣的文章
IJCAI阿里论文 | JUMP: 一种点击和停留时长的协同预估器
查看>>
腾讯十年投资记
查看>>
搭建直播平台需要从CDN“内部”入手
查看>>
python实现堆栈数据结构及其基本方法
查看>>
制造业瓶颈如何突破?“智变与突破——制造业人工智能产业峰会·南京”来献策...
查看>>
Linux shell 遍历
查看>>
MySQL ERROR 1372 (HY000): Password hash should be a 41-digit hexadecimal number
查看>>
如何设计一个高可用的运营系统
查看>>
SQL数据库学习之路(一)
查看>>
Prometheus监控实践:Kubernetes集群监控
查看>>
创建表格存储(Table Store)结果表
查看>>
网站301跳转
查看>>
Android 权限管理
查看>>
Django配置MySQL数据库及采坑记
查看>>
关于DialogFragment全屏方案
查看>>
WPF中查看PDF文件
查看>>
旷视科技 CSG 算法负责人姚聪:深度学习时代的文字检测与识别技术 | AI 研习社 103 期大讲堂...
查看>>
构建docker镜像
查看>>
《未来简史》读后感
查看>>
Flink靠什么征服饿了么工程师?
查看>>