public partial class AppDelegate : UIApplicationDelegate
{
  public AppDelegate () : base()
  {
    //---- set any user default values (they're not actually set until 
    // the settings application is run for your app)
    NSUserDefaults.StandardUserDefaults.SetBool (true, "staySignedin");
    NSUserDefaults.StandardUserDefaults.SetString ("blue", "favoriteColor");
  
    //---- initialize our user settings, which loads them from the file (if they exist)
    NSUserDefaults.StandardUserDefaults.Init ();
  }
  
  // This method is invoked when the application has loaded its UI and its ready to run
  public override bool FinishedLaunching (UIApplication app, NSDictionary options)
  {
    // If you have defined a view, add it here:
    // window.AddSubview (navigationController.View);
    window.MakeKeyAndVisible ();
    
    //---- wire up keyboard dismissal and focus
    this.txtUsername.ShouldReturn += this.DismissKeyboard;
    this.txtPassword.ShouldReturn += this.DismissKeyboard;
    this.txtFavoriteBand.ShouldReturn += this.DismissKeyboard;
    
    //---- wire up our save button
    this.btnSaveSettings.TouchUpInside += BtnSaveSettingsTouchUpInside;
    
    //---- populate page from settings
    this.PopulateSettings ();
    
    return true;
  }

  /// <summary>
  /// Populates our page from settings
  /// </summary>
  protected void PopulateSettings ()
  {
    //---- can access through typed calls:
    this.txtUsername.Text = NSUserDefaults.StandardUserDefaults.StringForKey ("username");
    //---- can access the dictionary, but must check for null:
    if (NSUserDefaults.StandardUserDefaults["password"] != null)
    {
      this.txtPassword.Text = NSUserDefaults.StandardUserDefaults["password"].ToString ();
    }
    this.swtchStaySignedin.On = NSUserDefaults.StandardUserDefaults.BoolForKey ("staySignedin");
    this.lblFavoriteColor.Text = NSUserDefaults.StandardUserDefaults.StringForKey ("favoriteColor");
    this.lblFavoriteFood.Text = NSUserDefaults.StandardUserDefaults.StringForKey ("favoriteFood");
    this.txtFavoriteBand.Text = NSUserDefaults.StandardUserDefaults.StringForKey ("favoriteBand");      
  }

  /// <summary>
  /// Saves our settings
  /// </summary>
  protected void BtnSaveSettingsTouchUpInside (object sender, EventArgs e)
  {
    NSUserDefaults.StandardUserDefaults.SetString (string.IsNullOrEmpty (
        this.txtUsername.Text) ? "" : this.txtUsername.Text, "username");
    NSUserDefaults.StandardUserDefaults.SetString (string.IsNullOrEmpty (
        this.txtPassword.Text) ? "" : this.txtPassword.Text, "password");
    NSUserDefaults.StandardUserDefaults.SetBool (this.swtchStaySignedin.On, "staySignedIn");
    NSUserDefaults.StandardUserDefaults.SetString (string.IsNullOrEmpty(
        this.txtFavoriteBand.Text) ? "" : this.txtFavoriteBand.Text, "favoriteBand");
  }
  
  /// <summary>
  /// Dismisses the keyboard by calling ResignFirstResponder
  /// </summary>
  protected bool DismissKeyboard (UITextField textBox)
  {
    textBox.ResignFirstResponder ();
    return true;
  }

  
  // This method is required in iPhoneOS 3.0
  public override void OnActivated (UIApplication application)
  {
  }
}