技术开发 频道

详细解析WPF中的应用程序模型

  2. 使用Property Bag来存储联系人的集合

  (1)在程序启动的时候,应用程序必须从文件中读出联系人的信息,并初始化ContactList对象。打开App.xaml.cs文件并编辑。ReadContactsFromFile方法会从文件中读出数据。在类里添加这个方法,并添加一个辅助方法来把数据转化成联系人对象:

        //
        
// Reads contact information from file
        
//
        private ContactList ReadContactsFromFile()
        {
            ContactList contactList
= new ContactList();

            
// Create an instance of StreamReader to read from a file.
            
// The using statement also closes the StreamReader.
            using (StreamReader sr = new StreamReader("contacts.txt"))
            {
                String line;
                
// Read and display lines from the file until the
                
// end of the file is reached.
                while ((line = sr.ReadLine()) != null)
                {
                    contactList.Add(CreateContactFromLine(line));
                }
            }

            
return contactList;
        }
        
        
//
        
// De-tokenize one line of contact information and
        
// hydrate a Contact object
        
//
        private Contact CreateContactFromLine(string line)
        {
            
string[] tokens = line.Split(new char[] { ';' });

            
if (tokens.Length != 6)
            {
                
throw new ApplicationException(
                     String.Format(
"Input contact file format. " +
                    
"Expected tokens {0}; Actual tokens {1}", 6,
                     tokens.Length));
            }

            Contact contact
= new Contact();
            contact.FirstName
= tokens[0];
            contact.LastName
= tokens[1];
            contact.EmailAddress
= tokens[2];
            contact.HomePage
= (String.IsNullOrEmpty(tokens[3]) ?
                                
null :
                                
new Uri(tokens[3], UriKind.Absolute));
            contact.HomeAddress
= tokens[4];
            contact.BusinessAddress
= tokens[5];

            
return contact;
        }

 

  ReadContactFromFile方法使用System.IO.StreamReader。请确认您在命名空间声明处使用了System.IO:

  using System.IO;

  (2)下面我们把ReadContactsFromFile方法返回的ContactList数据,添加到应用程序Property Bag里面。如下修改AppStartup方法:

       //
      
// Triggered on application startup. Positions the window,
      
// Initializes the contact list model and adds it to the
      
// Property Bag.
      
//
        void AppStartup(object sender, StartupEventArgs args)
        {
          
// initialize the Contacts collection using data from file
           ContactList contactList = ReadContactsFromFile();

          
// add it to the Property Bag
           this.Properties["ContactList"] = contactList;

            MainWindow mainWindow
= new MainWindow();
            
// make sure the window appears in the center of the screen
            mainWindow.WindowStartupLocation =
                                   WindowStartupLocation.CenterScreen;
            mainWindow.Show();
         }

 

  (3)让我们顺便在这里创建一个方法,来把联系人数据的更新存回到contacts.txt文件里。因为逻辑非常简单且与本实验关系不大,我们这里就不去实现它了:

        //
        
// Persists changes from ContactList object in Property Bag
        
// to file.
        
//
        private void SaveContactsToFile(string fileName)
        {
        }

 

  (4) 生成并运行应用程序。此时的UI看上去应该和上一个任务结束时没什么不同,看上去我们好像没有什么进展。别着急!

1
相关文章