技术开发 频道

手把手带你从WPF基础向企业应用进阶

  公用代码部分(BusinessObjects)

  这两个应用程序都是使用的BusinessObjects作为逻辑类库,BusinessObjects中的Company对UI所使用的数据进行了Mock。所以他们在需求方面都是一样的,由于比较简单,所以请看下面代码:

Collapse
using System;
using System.ComponentModel;
using System.IO;
using System.Reflection;

namespace BusinessObjects
{
public class Company : INotifyPropertyChanged
{
    
#region Creation

    
public static Company[] GetCompanys()
    {
        
// In a real app this would probably call into a data access layer to get records from a database.
        
return new Company[]
        {
            
new Company(1, "博客园", "CNBlogs", GetPictureFile(1), new DateTime(2004, 1, 12)),
            
new Company(2, "51CTO", "51CTO", GetPictureFile(2), new DateTime(2005, 3, 1)),
            
new Company(3, "CSDN", "CSDN", GetPictureFile(3), new DateTime(2000, 1, 20)),
        };
    }

    
private static string GetPictureFile(int CompanyID)
    {
        
string fileName = String.Format("emp{0}.jpg", CompanyID);
        
string folder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
        folder
= Path.Combine(folder, "Images");
        
return Path.Combine(folder, fileName);
    }

    
private Company(int id, string chineseName, string EnglishName, string pictureFile, DateTime startDate)
    {
        this.ID
= id;
        this.chineseName
= chineseName;
        this.EnglishName
= EnglishName;
        this.PictureFile
= pictureFile;
        this.StartDate
= startDate;
    }

    #endregion
// Creation

    
#region Properties

    
public int ID { get; private set; }

    
string _chineseName;
    
public string chineseName
    {
        
get { return _chineseName; }
        
set
        {
            
if (value == _chineseName)
                
return;

            _chineseName
= value;

            this.OnPropertyChanged(
"chineseName");
            this.OnPropertyChanged(
"FullName");
        }
    }

    
string _EnglishName;
    
public string EnglishName
    {
        
get { return _EnglishName; }
        
set
        {
            
if (value == _EnglishName)
                
return;

            _EnglishName
= value;

            this.OnPropertyChanged(
"EnglishName");
            this.OnPropertyChanged(
"FullName");
        }
    }

    
public string FullName
    {
        
get { return String.Format("{0}, {1}", this.EnglishName, this.chineseName); }
    }

    
public string PictureFile { get; private set; }
    
public DateTime StartDate { get; private set; }

    #endregion
// Properties

    
#region INotifyPropertyChanged Members

    
public event PropertyChangedEventHandler PropertyChanged;

    
protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler
= this.PropertyChanged;
        
if (handler != null)
            handler(this,
new PropertyChangedEventArgs(propertyName));
    }

    #endregion
}
}

 

  上面这段代码没有什么不寻常的地方,大家写WinForm和Asp.Net也会写这样的逻辑类,只是要注意Company 实现了INotifyPropertyChanged 接口,大家看到这个接口只有一个OnPropertyChanged的方法,这个方法就是我们要说的属性变更通知方法,就是说当一个属性改变了,我们需要做些什么来响应这些改变。

0