`

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.

 

 

 

 

分享到:
评论

相关推荐

    wpf 研习1-24小时自学wpf6

    本文将围绕“wpf 研习1-24小时自学wpf6”这一主题,探讨WPF的核心概念和技术,旨在帮助初学者快速上手并深入理解这一强大的UI框架。 WPF是微软.NET Framework的一部分,它提供了全面的图形系统,包括2D和3D渲染、...

    wpf 研习1-24小时自学wpf9

    【标题】:“WPF研习1-24小时自学WPF9” 在Windows Presentation Foundation(WPF)的世界里,这是一段深入学习之旅的开端。WPF是.NET Framework的一个重要组成部分,它为创建丰富的、高性能的桌面应用程序提供了...

    wpf 研习1-24小时自学wpf7

    标题 "wpf 研习1-24小时自学wpf7" 提示我们这是一个关于Windows Presentation Foundation(WPF)的学习资源,可能是书籍、课程或教程的一部分,旨在帮助初学者在24小时内掌握WPF的基础知识。WPF是.NET Framework中的...

    wpf 研习1-24小时自学wpf8

    【标题】:“wpf 研习1-24小时自学wpf8”是指一系列针对Windows Presentation Foundation(WPF)框架的学习教程,旨在帮助初学者在24小时内掌握WPF的基础知识和应用技巧。WPF是.NET Framework的一部分,用于构建...

    wpf 研习1-24小时自学wpf3

    NULL 博文链接:https://x-dome.iteye.com/blog/684355

    C#自学最好的课件资源

    对于想要开发Windows应用的学员,WinForms和WPF(Windows Presentation Foundation)的使用也会有所介绍。 在幻灯片部分,可能会包含每章节的重点摘要,清晰的示例图解,以及关键概念的解释,帮助学生更好地吸收和...

Global site tag (gtag.js) - Google Analytics