MonoTouch.Dialog无法获取文本字段上的值

时间:2011-02-10 00:29:50

标签: c# iphone xamarin.ios monotouch.dialog

我有以下代码

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

我运行应用程序然后我填写文本域但是当我点击TapMe时我得到一个空值,那么如何使用monotouch.dialog检索文本字段上的值?

2 个答案:

答案 0 :(得分:4)

您需要首先在绑定上下文中调用Fetch(),如下所示:

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

答案 1 :(得分:0)

_loginbindingcontext.Fetch();

这将使用LogInViewModel中元素的值填充DialogViewController

您需要在ViewModel的{​​{1}}中保留一个引用,以便拨打BindingContext

或者,您可以在GitHub MonoTouch.MVVM上查看我的项目,这使得这些事情变得更容易。它还没有完成,它只是一个概念证明,但我正积极致力于它。 https://github.com/RobertKozak/MonoTouch.MVVM