Chinaunix首页 | 论坛 | 博客
  • 博客访问: 6535495
  • 博文数量: 915
  • 博客积分: 17977
  • 博客等级: 上将
  • 技术积分: 8846
  • 用 户 组: 普通用户
  • 注册时间: 2005-08-26 09:59
个人简介

一个好老好老的老程序员了。

文章分类

全部博文(915)

文章存档

2022年(9)

2021年(13)

2020年(10)

2019年(40)

2018年(88)

2017年(130)

2015年(5)

2014年(12)

2013年(41)

2012年(36)

2011年(272)

2010年(1)

2009年(53)

2008年(65)

2007年(47)

2006年(81)

2005年(12)

分类: Android平台

2019-11-12 17:01:10

ViewModel中的交互式属性
ViewModel的第二个例子做了一些非常基本的事情,你永远不会为此目的编写ViewModel。 SimpleMultiplierViewModel类简单地将两个数字相乘。 但它是一个很好的例子,用于演示具有多个交互属性的ViewModel的开销和机制。 (虽然你永远不会编写一个ViewModel来将两个数字相乘,但你可以编写一个ViewModel来解决二次方程或更复杂的东西。)
SimpleMultiplierViewModel类是SimpleMultiplier项目的一部分:

点击(此处)折叠或打开

  1. using System;
  2. using System.ComponentModel;
  3. namespace SimpleMultiplier
  4. {
  5.     class SimpleMultiplierViewModel : INotifyPropertyChanged
  6.     {
  7.         double multiplicand, multiplier, product;
  8.         public event PropertyChangedEventHandler PropertyChanged;
  9.         public double Multiplicand
  10.         {
  11.             set
  12.             {
  13.                 if (multiplicand != value)
  14.                 {
  15.                     multiplicand = value;
  16.                     OnPropertyChanged("Multiplicand");
  17.                     UpdateProduct();
  18.                 }
  19.             }
  20.             get
  21.             {
  22.                 return multiplicand;
  23.             }
  24.         }
  25.         public double Multiplier
  26.         {
  27.             set
  28.             {
  29.                 if (multiplier != value)
  30.                 {
  31.                     multiplier = value;
  32.                     OnPropertyChanged("Multiplier");
  33.                     UpdateProduct();
  34.                 }
  35.             }
  36.             get
  37.             {
  38.                 return multiplier;
  39.             }
  40.         }
  41.         public double Product
  42.         {
  43.             protected set
  44.             {
  45.                 if (product != value)
  46.                 {
  47.                     product = value;
  48.                     OnPropertyChanged("Product");
  49.                 }
  50.             }
  51.             get
  52.             {
  53.                 return product;
  54.             }
  55.         }
  56.         void UpdateProduct()
  57.         {
  58.             Product = Multiplicand * Multiplier;
  59.         }
  60.         protected void OnPropertyChanged(string propertyName)
  61.         {
  62.             PropertyChangedEventHandler handler = PropertyChanged;
  63.             if (handler != null)
  64.             {
  65.                 PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
  66.             }
  67.         }
  68.     }
  69. }

  

阅读(932) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~