2

I have the following code

public partial class AppDelegate : UIApplicationDelegate
    {
        UINavigationController _navigationController = new UINavigationController();

        public enum PrivacySetting { always, never, friends };

        LogInViewModel _login;

        // 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);

            _login = new LogInViewModel
            {
                Privacy = PrivacySetting.always
            };

            var _loginbindingcontext = new BindingContext(this, _login, "Login");
            var dialogcontroller = new DialogViewController(_loginbindingcontext.Root);

            _navigationController.PushViewController(dialogcontroller, true);

            window.AddSubview(_navigationController.View);

            window.MakeKeyAndVisible ();

            return true;
        }

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

        public class LogInViewModel
        {

            [Section("Credentials")]

            [Entry("Username")]
            public string login;

            [Caption("Password"), Password("Password")]
            public string password;

            [Section("Privacy")]

            [Caption("Show Name")]
            public PrivacySetting Privacy;

            [Section ("Tap to Console")]
            [OnTap ("tapme")]
            public string TapMe;
        }

        public void tapme()
        {
            Console.WriteLine(_login.login);    
        }
    }

i run the application then i fill in the textfields but when i tap on TapMe i get a null value, so how can i retrieve values on textfields using monotouch.dialog?

Joe Dixon
  • 1,557
  • 2
  • 10
  • 14

2 Answers2

4

You need to first call Fetch() on the binding context, like this:

public void tapme()
{
            _loginbindingcontext.Fetch();
            Console.WriteLine(_login.login);    
}
Eduardo Scoz
  • 24,653
  • 6
  • 47
  • 62
0
_loginbindingcontext.Fetch();

This will populate the LogInViewModel with the values from the Elements in the DialogViewController.

You will need to keep a reference in your ViewModel for the BindingContext so that you can call Fetch().

Alternatively, you can check out my project on GitHub MonoTouch.MVVM which makes these kinds of things easier. Its not done yet and its only a proof of concept but I am actively working on it. https://github.com/RobertKozak/MonoTouch.MVVM

SoftSan
  • 2,482
  • 3
  • 23
  • 54
Robert Kozak
  • 2,043
  • 17
  • 33
  • Do you have any blog where MVVM its documented? – dalexsoto Feb 10 '11 at 02:06
  • Not yet. But I will. I have some more work to do on it first before I start documenting it. The design is going thru some changes so I don't want to document it yet until it stabilizes. I'll have a big update in the next couple of weeks. – Robert Kozak Feb 10 '11 at 18:15