wpf 研习1-24小时自学wpf12

A Contact Manager

 

Choosing an Architecture-interactive application

MVC-

Model objects represent the business concerns that are specific to the application being built;

Views are the actual visible components of an application;

Controllers traditionally functioned as mediators between the human and the application by handling user input from the mouse or keyboard(many of the responsibilities that the Controller had in the original
pattern have been taken over by WPF’s rich control set);

 

MVP- 

Model/View/Presenter,evolved from MVC;(M-primary purpose is to represent business-specific constructs in software;Supervising-Controller pattern,Passive-View pattern)

 

Expander,allow the user to collapse or expand a named region of the interface,use the Header property to label the region.The Header property is of type object.(RotateTransform)

 

Slider,property-Minimum,Maximum,Interval, SmallChange, and LargeChange

 

TabControl,inherits from ItemsControl,most commonly used to display a collection of TabItem controls;

the TabControl can be scaled to the user’s preference via the data binding between the ScaleTransform and the Slider;

 

Aggregate

Notifier : INotifyPropertyChanged(which  is property changed notification mechanism,enables more powerful data binding in WPF.)

Address : Notifier

 

//notifier.cs
using System;
using System.ComponentModel;

namespace ContactManager
{
    [Serializable]
    public abstract class Notifier : INotifyPropertyChanged
    {
        [field: NonSerialized]
        public event PropertyChangedEventHandler PropertyChanged;

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

//address.cs
using System;

namespace ContactManager.Model
{
    [Serializable]
    public class Address : Notifier
    {
        private string _city;
        private string _country;
        private string _line1;
        private string _line2;
        private string _state;
        private string _zip;

        public string City
        {
            get { return _city; }
            set
            {
                _city = value;
                OnPropertyChanged("City");
            }
        }

        public string Country
        {
            get { return _country; }
            set
            {
                _country = value;
                OnPropertyChanged("Country");
            }
        }

        public string Line1
        {
            get { return _line1; }
            set
            {
                _line1 = value;
                OnPropertyChanged("Line1");
            }
        }

        public string Line2
        {
            get { return _line2; }
            set
            {
                _line2 = value;
                OnPropertyChanged("Line2");
            }
        }

        public string State
        {
            get { return _state; }
            set
            {
                _state = value;
                OnPropertyChanged("State");
            }
        }

        public string Zip
        {
            get { return _zip; }
            set
            {
                _zip = value;
                OnPropertyChanged("Zip");
            }
        }
    }
}

//states.cs
using System.Collections.Generic;

namespace ContactManager.Model
{
    public static class States
    {
        private static readonly List<string> _names;

        static States()
        {
            _names = new List<string>(50);

            _names.Add("Alabama");
            _names.Add("Alaska");
            _names.Add("Arizona");
            _names.Add("Arkansas");
            _names.Add("California");
            _names.Add("Colorado");
            _names.Add("Connecticut");
            _names.Add("Delaware");
            _names.Add("Florida");
            _names.Add("Georgia");
            _names.Add("Hawaii");
            _names.Add("Idaho");
            _names.Add("Illinois");
            _names.Add("Indiana");
            _names.Add("Iowa");
            _names.Add("Kansas");
            _names.Add("Kentucky");
            _names.Add("Louisiana");
            _names.Add("Maine");
            _names.Add("Maryland");
            _names.Add("Massachusetts");
            _names.Add("Michigan");
            _names.Add("Minnesota");
            _names.Add("Mississippi");
            _names.Add("Missouri");
            _names.Add("Montana");
            _names.Add("Nebraska");
            _names.Add("Nevada");
            _names.Add("New Hampshire");
            _names.Add("New Jersey");
            _names.Add("New Mexico");
            _names.Add("New York");
            _names.Add("North Carolina");
            _names.Add("North Dakota");
            _names.Add("Ohio");
            _names.Add("Oklahoma");
            _names.Add("Oregon");
            _names.Add("Pennsylvania");
            _names.Add("Rhode Island");
            _names.Add("South Carolina");
            _names.Add("South Dakota");
            _names.Add("Tennessee");
            _names.Add("Texas");
            _names.Add("Utah");
            _names.Add("Vermont");
            _names.Add("Virginia");
            _names.Add("Washington");
            _names.Add("West Virginia");
            _names.Add("Wisconsin");
            _names.Add("Wyoming");
        }

        public static IList<string> GetNames()
        {
            return _names;
        }
    }
}

 

 

 

Abstract Data Store

Regardless of what the underlying data store is, every well-architected solution hides this technicality from the rest of the application.

A common pattern is to execute all datarelated activity through a repository class(Separation of Concerns (SoC)-The important part of a repository is that it abstracts away the actual data store so that other parts of the application are able to work with a high-level API and not concern themselves with how the data is stored.);

 

//ContactRepository.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;

namespace ContactManager.Model
{
  public class ContactRepository
  {
    private List<Contact> _contactStore;
    private readonly string _stateFile;
    public ContactRepository()
    {
      _stateFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory,"ContactManager.state");
      Deserialize();
     }
    public void Save(Contact contact)
    {
      if (contact.Id == Guid.Empty)
          contact.Id = Guid.NewGuid();
      if (!_contactStore.Contains(contact))
          _contactStore.Add(contact);
      Serialize();
     }
    public void Delete(Contact contact)
    {
      _contactStore.Remove(contact);
      Serialize();
     }
     public List<Contact> FindByLookup(string lookupName)
     {
       IEnumerable<Contact> found = from c in _contactStore
                                      where c.LookupName.StartsWith(
                                      lookupName,
                                      StringComparison.OrdinalIgnoreCase)  select c;
       return found.ToList();
      }
     public List<Contact> FindAll()
     {
       return new List<Contact>(_contactStore);
      }  
     private void Serialize()
    {
      using (FileStream stream =File.Open(_stateFile, FileMode.OpenOrCreate))
     {
        BinaryFormatter formatter = new BinaryFormatter();
        formatter.Serialize(stream, _contactStore);
      }
     }
     private void Deserialize()
      {
       if (File.Exists(_stateFile))
       {
          using (FileStream stream =File.Open(_stateFile, FileMode.Open))
          {
             BinaryFormatter formatter = new BinaryFormatter();
             _contactStore =(List<Contact>)formatter.Deserialize(stream);
          }
        }
       else _contactStore = new List<Contact>();
      }
 }
}

 

a repository, is a type of class that hides the actual data storage mechanism from the rest of the application. It embodies SoC by separating any datarelated code from other classes and encapsulating it in a way that hides the internal complexity from unconcerned parties.

 

小结:

wpf,is real power.

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值