2013-03-18 81 views
1

我正在使用MonoTouch開發我的iPhone應用程序。我想使用Monotouch。用於向客戶端顯示一些數據的對話框,讓他們更改數據,然後再將它們保存到文件中。將Monotouch.Dialog元素值保存到文件中

我的代碼是一樣的東西Xamarin教程代碼: (Orginal Sample link)

public enum Category 
{ 
    Travel, 
    Lodging, 
    Books 
} 

public class ExpesObject{ 
    public string name; 
} 

public class Expense 
{ 
    [Section("Expense Entry")] 

    [Entry("Enter expense name")] 
    public string Name; 
    [Section("Expense Details")] 

    [Caption("Description")] 
    [Entry] 
    public string Details; 
    [Checkbox] 
    public bool IsApproved = true; 
    [Caption("Category")] 
    public Category ExpenseCategory; 
} 

它代表TableView那麼好。 但問題是,我們如何保存這些元素的數據並將它們用於其他類的應用程序?這樣做的最好方法是什麼? 我想我們可以在用戶更改數據時將數據保存到文件中。但我們如何檢測用戶何時更改數據?

回答

2

在示例中,您使用Monotouch.Dialog的簡單Reflection API。雖然這很好很容易,但它確實限制了你可以做的事情。我建議學習使用Monotouch.Dialog的這裏看到的(http://docs.xamarin.com/guides/ios/user_interface/monotouch.dialog/elements_api_walkthrough)元素API,它將給你對錶中每個項目的更多控制,並且能夠檢測到變化等。

每個表格單元格(例如Name是一個可以編輯的單元格)具有某些事情發生時的動作/事件,例如正在更改文本。

例如,上面的屏幕可以使用元素API進行以下操作。

public class ExpenseViewController : DialogViewController 
{ 
    EntryElement nameEntry; 

    public ExpenseViewController() : base(null, true) 
    { 
     Root = CreateRootElement(); 

     // Here is where you can handle text changing 
     nameEntry.Changed += (sender, e) => { 
      SaveEntryData(); // Make your own method of saving info. 
     }; 
    } 

    void CreateRootElement(){ 
     return new RootElement("Expense Form"){ 
      new Section("Expense Entry"){ 
       (nameEntry = new EntryElement("Name", "Enter expense name", "", false)) 
      }, 
      new Section("Expense Details"){ 
       new EntryElement("Description", "", "", false), 
       new BooleanElement("Approved", false, ""), 
       new RootElement("Category", new Group("Categories")){ 
        new CheckboxElement("Travel", true, "Categories"), 
        new CheckboxElement("Personal", false, "Categories"), 
        new CheckboxElement("Other", false, "Categories") 
       } 
      } 
     }; 
    } 

    void SaveEntryData(){ 
     // Implement some method for saving data. e.g. to file, or to a SQLite database. 
    } 

} 

考慮這些地區開始使用API​​的元素開始: 來源:https://github.com/migueldeicaza/MonoTouch.Dialog

MT.D介紹:http://docs.xamarin.com/guides/ios/user_interface/monotouch.dialog

MT.D要素演練:http://docs.xamarin.com/guides/ios/user_interface/monotouch.dialog/elements_api_walkthrough