码迷,mamicode.com
首页 > 移动开发 > 详细

Xamarin.iOS常用控件总结

时间:2015-11-18 22:50:35      阅读:251      评论:0      收藏:0      [点我收藏+]

标签:

1.UIButton控件

btn.SetTitle("test", UIControlState.Normal);

UIControlState枚举类型使用:

  • Normal:默认可使用状态
  • Highlighted:当点击控件事件时控件的状态
  • Disabled:控件状态不可用
  • Selected:控件选中时的状态
  • Application: 使用Application的一个附加控件状态
  • Reserved:
UIButton btn = UIButton.FromType(UIButtonType.System);

UIButtonType枚举类型:

  • System
  • Custom
  • RoundedRect
  • DetailDisclosure
  • InfoLight
  • InfoDark
  • ContactAdd

2.UIImageView

UIImageView image = new UIImageView();
            image.Image = UIImage.FromFile("test.jpg");
            image.ContentMode = UIViewContentMode.Center;

UIViewContentMode枚举类型:

  • ScaleToFill
  • ScaleAspectFit
  • ScaleAspectFill
  • Redraw
  • Center
  • Top,Bottom,Left,Rigth,TopLeft,TopRight,BottomLeft,BottomRight

3.UITextView控件,显示和编辑文本

base.ViewDidLoad();
            UIButton btn = new UIButton();
            UITextView myTextView = new UITextView();
            //....省略部分代码
            btn.Enabled = false;
            btn.TouchUpInside += (object sender, EventArgs e) =>
            {
                myTextView.ResignFirstResponder();//隐藏键盘
            };
            myTextView.Delegate = new MyTextViewDelegate(this);
private class MyTextViewDelegate : UITextViewDelegate
        {

            public MyTextViewDelegate (TextViewAppViewController parentController)
            {
                this.parentController = parentController;
            }
            private TextViewAppViewController parentController;

            public override void EditingStarted (UITextView textView)
            {
                this.parentController.btn.Enabled = true;
            }

            public override void EditingEnded (UITextView textView)
            {
                this.parentController.btn.Enabled = false;
            }

            public override void Changed (UITextView textView)
            {
                Console.WriteLine ("Text changed!");
            }

        }//end void MyTextViewDelegate

4.UITextField控件,UIKeyboardType枚举类型,使用键盘

private NSObject kbdWillShow, kbdDidHide;
        public override void ViewDidLoad()
        {

            base.ViewDidLoad();
            UITextField emailField = new UITextField();
            this.emailField.KeyboardType = UIKeyboardType.EmailAddress;
            this.emailField.ReturnKeyType = UIReturnKeyType.Done;

            // The Xamarin.iOS way.添加Observers,防止键盘阻挡输入框
            this.kbdWillShow = UIKeyboard.Notifications.ObserveWillShow((s, e) => {

                RectangleF kbdBounds = e.FrameEnd;
                RectangleF textFrame = this.emailField.Frame;

                textFrame.Y -= kbdBounds.Height;
                this.emailField.Frame = textFrame;

            });

            this.kbdDidHide = UIKeyboard.Notifications.ObserveDidHide((s, e) => {

                RectangleF kbdBounds = e.FrameEnd;
                RectangleF textFrame = this.emailField.Frame;

                textFrame.Y += kbdBounds.Height;
                this.emailField.Frame = textFrame;

            });

            // The "iOS-way".
//            this.kbdWillShow = NSNotificationCenter.DefaultCenter.AddObserver (UIKeyboard.WillShowNotification, delegate(NSNotification ntf) {
//
//                RectangleF kbdBounds = UIKeyboard.FrameEndFromNotification (ntf);
//
//                RectangleF textFrame = this.emailField.Frame;
//
//                textFrame.Y -= kbdBounds.Height;
//                this.emailField.Frame = textFrame;
//
//            });

//            this.kbdDidHide = NSNotificationCenter.DefaultCenter.AddObserver (UIKeyboard.DidHideNotification, delegate(NSNotification ntf) {
//
//                RectangleF kbdBounds = UIKeyboard.FrameEndFromNotification (ntf);
//
//                RectangleF textFrame = this.emailField.Frame;
//
//                textFrame.Y += kbdBounds.Height;
//                this.emailField.Frame = textFrame;
//
//            } );

            this.emailField.ShouldReturn = delegate(UITextField textField) {
                return textField.ResignFirstResponder ();
            };

        }

 

NSNotificationCenter.DefaultCenter.RemoveObserver
(this.kbdWillShow);
NSNotificationCenter.DefaultCenter.RemoveObserver
(this.kbdDidHide);

5.UIProgressView控件

this.buttonStartProgress.SetTitle ("Tap to start progress!", UIControlState.Normal);
            this.buttonStartProgress.TouchUpInside += delegate {
                // Disable the button
                this.buttonStartProgress.Enabled = false;
                this.progressView.Progress = 0f;
                // Start a progress
                Task.Factory.StartNew(this.StartProgress);
            } ;

            // Initialize the progress view
            this.progressView = new UIProgressView (new RectangleF (60f, 200f, 200f, 50f));            

            // Set the progress view‘s initial value
            this.progressView.Progress = 0f;

            // Set the progress increment value
            // for 10 items
            this.incrementBy = 1f / 10f;

            // Display the controls
            this.View.AddSubview(this.labelStatus);
            this.View.AddSubview(this.buttonStartProgress);
            this.View.AddSubview(this.progressView);
private void StartProgress ()
        {

            float currentProgress = 0f;
            while (currentProgress < 1f)
            {

                Thread.Sleep(1000);

                this.InvokeOnMainThread(delegate {

                    // Advance the progress
                    this.progressView.Progress += this.incrementBy;
                    currentProgress = this.progressView.Progress;

                    // Set the label text
                    this.labelStatus.Text = string.Format("Current value: {0}", Math.Round((double)this.progressView.Progress, 2));

                    if (currentProgress >= 1f)
                    {
                        this.labelStatus.Text = "Progress completed!";
                        this.buttonStartProgress.Enabled = true;
                    }//end if

                });

            }//end while

6.UIScrollView控件

常用属性:

  • ContentSize
  • ContentOffset
  • PagingEnabled

常用事件:

  • Scrolled
  • DecelerationStarted
  • DecelerationEnded

Xamarin.iOS常用控件总结

标签:

原文地址:http://www.cnblogs.com/zjmsky/p/4975975.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!