• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

c# - MonoTouch.Dialog - 接受空值作为输入的 DateElement

[复制链接]
菜鸟教程小白 发表于 2022-12-11 22:42:56 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我在互联网上搜索无济于事...我正在使用 MT.D 并想为使用 DateElement 的人设置生日,但生日可能为空,这意味着数据尚未收集。有人知道如何让 DateElement 接受空值或日期吗?



Best Answer-推荐答案


更新 20140106:自从 iOS7 发布以来,Apple 希望日期/时间选择器与内容一致,而不是作为操作表,或者在本例中为全屏覆盖。因此,此代码仅用于说明和历史目的。

好的,所以我推出了自己的类(class)。就我个人而言,我认为当前的日期/时间选择器设置看起来并不像弹出一个带有日期选择器的 ActionSheet 等效项那样专业。在 MT.D 方面经验更丰富的人可能能够弄清楚,但我所做的是从 DateTimeElementDateElement 复制代码并对其进行修改,使其具有三个顶部按钮:最左边的按钮是取消,右边的按钮区域有“设置”和“空”按钮。右侧按钮的标题可以在类的ctor中设置为您喜欢的任何内容,但可以默认为“设置日期”和“无日期”。

分享就是关怀!

可为空的日期时间元素

using MonoTouch.Foundation;
using MonoTouch.UIKit;
using System;
using System.Drawing;

namespace MonoTouch.Dialog
{
    public class NullableDateTimeElement : StringElement
    {
        private class MyViewController : UIViewController
        {
            private NullableDateTimeElement container;
            private bool hasNullValue = false;
            private bool hasBeenSet = false;
            //private EventHandler nullButtonTouched;
            //UIButton isNullButton;
            public bool Autorotate
            {
                get;
                set;
            }
            public MyViewController (NullableDateTimeElement container)
            {
                this.container = container;
            }
            public override void ViewDidLoad ()
            {
                base.ViewDidLoad ();
                //isNullButton = UIButton.FromType (UIButtonType.RoundedRect);
                //isNullButton.SizeToFit ();
                //isNullButton.Frame = new RectangleF(this.View.Frame.Top, this.View.Frame.Left, this.View.Frame.Width - 40f, 40f);
                //isNullButton.SetTitle (container.NullButtonCaption, UIControlState.Normal);
                this.NavigationItem.RightBarButtonItems = new UIBarButtonItem[]
                {
                    new UIBarButtonItem(container.NullButtonCaption, UIBarButtonItemStyle.Done, NullButtonTapped),
                    new UIBarButtonItem(container.SetButtonCaption, UIBarButtonItemStyle.Done, SetButtonTapped)
                };
                this.NavigationItem.LeftBarButtonItem = new UIBarButtonItem(UIBarButtonSystemItem.Cancel, CancelTapped);
                this.NavigationItem.HidesBackButton = true;
                //this.View.AddSubview (isNullButton);
                //this.isNullButton.TouchUpInside += (nullButtonTouched = new EventHandler(nullButtonWasTouched)); 
            }

            void CancelTapped(object sender, EventArgs e)
            {
                hasBeenSet = false;
                this.NavigationController.PopViewControllerAnimated (true);
            }

            void NullButtonTapped(object sender, EventArgs e)
            {
                hasBeenSet = true;
                hasNullValue = true;
                this.NavigationController.PopViewControllerAnimated (true);
            }

            void SetButtonTapped(object sender, EventArgs e)
            {
                hasBeenSet = true;
                hasNullValue = false;
                this.NavigationController.PopViewControllerAnimated (true);
            }

            public override void ViewWillDisappear (bool animated)
            {
                base.ViewWillDisappear (animated);
                if (hasBeenSet)
                {
                    if (!hasNullValue)
                        this.container.DateValue = this.container.datePicker.Date;
                    else
                        this.container.DateValue = null;
                }
                //this.isNullButton.TouchUpInside -= nullButtonTouched;
                //nullButtonTouched = null;
            }
            /*void nullButtonWasTouched(object sender, EventArgs e)
            {
                hasNullValue = true;
                NavigationController.PopViewControllerAnimated (true);
            }*/
            public override void DidRotate (UIInterfaceOrientation fromInterfaceOrientation)
            {
                base.DidRotate (fromInterfaceOrientation);
                this.container.datePicker.Frame = NullableDateTimeElement.PickerFrameWithSize (this.container.datePicker.SizeThatFits (SizeF.Empty));
            }
            public override bool ShouldAutorotateToInterfaceOrientation (UIInterfaceOrientation toInterfaceOrientation)
            {
                return this.Autorotate;
            }
        }
        public DateTime? DateValue;
        public UIDatePicker datePicker;
        //public UIButton isNullButton;
        public string NullButtonCaption { get; set; }
        public string SetButtonCaption { get; set; }

        protected internal NSDateFormatter fmt = new NSDateFormatter
        {
            DateStyle = NSDateFormatterStyle.Short
        };

        public NullableDateTimeElement (string caption, DateTime? date, string nullButtonCaption, string setButtonCaption) : base (caption)
        {
            this.DateValue = date;
            this.Value = this.FormatDate (date);
            this.NullButtonCaption = nullButtonCaption;
            this.SetButtonCaption = setButtonCaption;
        }

        public NullableDateTimeElement(string caption, DateTime? date, string nullButtonCaption) : this(caption, date, nullButtonCaption, "Set Date")
        {}

        public NullableDateTimeElement(string caption, DateTime? date) : this(caption, date, "No Date", "Set Date")
        {}

        public override UITableViewCell GetCell (UITableView tv)
        {
            this.Value = this.FormatDate (this.DateValue);
            UITableViewCell cell = base.GetCell (tv);
            cell.Accessory = UITableViewCellAccessory.DisclosureIndicator;
            return cell;
        }
        protected override void Dispose (bool disposing)
        {
            base.Dispose (disposing);
            if (disposing)
            {
                if (this.fmt != null)
                {
                    this.fmt.Dispose ();
                    this.fmt = null;
                }
/*              if (this.isNullButton != null)
                {
                    this.isNullButton.Dispose ();
                    this.isNullButton = null;
                }*/
                if (this.datePicker != null)
                {
                    this.datePicker.Dispose ();
                    this.datePicker = null;
                }
            }
        }
        public virtual string FormatDate (DateTime? dt)
        {
            if (dt.HasValue)
                return this.fmt.ToString (dt.Value) + " " + dt.Value.ToLocalTime ().ToShortTimeString ();
            else
                return NullButtonCaption;
        }
        public virtual UIDatePicker CreatePicker ()
        {
            return new UIDatePicker (RectangleF.Empty)
            {
                AutoresizingMask = UIViewAutoresizing.FlexibleWidth,
                Mode = UIDatePickerMode.DateAndTime,
                Date = this.DateValue ?? DateTime.Now
            };
        }
        private static RectangleF PickerFrameWithSize (SizeF size)
        {
            RectangleF applicationFrame = UIScreen.MainScreen.ApplicationFrame;
            float y = 0f;
            float x = 0f;
            switch (UIApplication.SharedApplication.StatusBarOrientation)
            {
                case UIInterfaceOrientation.Portrait:
                case UIInterfaceOrientation.PortraitUpsideDown:

                    {
                        x = (applicationFrame.Width - size.Width) / 2f;
                        y = (applicationFrame.Height - size.Height) / 2f - 25f;
                        break;
                    }
                case UIInterfaceOrientation.LandscapeRight:
                case UIInterfaceOrientation.LandscapeLeft:

                    {
                        x = (applicationFrame.Height - size.Width) / 2f;
                        y = (applicationFrame.Width - size.Height) / 2f - 17f;
                        break;
                    }
            }
            return new RectangleF (x, y, size.Width, size.Height);
        }
        public override void Selected (DialogViewController dvc, UITableView tableView, NSIndexPath path)
        {
            NullableDateTimeElement.MyViewController myViewController = new NullableDateTimeElement.MyViewController (this)
            {
                Autorotate = dvc.Autorotate
            };
            this.datePicker = this.CreatePicker ();
            this.datePicker.Frame = NullableDateTimeElement.PickerFrameWithSize (this.datePicker.SizeThatFits (SizeF.Empty));
            myViewController.View.BackgroundColor = UIColor.Black;
            myViewController.View.AddSubview (this.datePicker);
            dvc.ActivateController (myViewController);
        }
    }
}

NULLABLE DATE-ONLY ELEMENT

using System;
using MonoTouch.Foundation;
using MonoTouch.UIKit;


namespace MonoTouch.Dialog
{
    public class NullableDateElement : NullableDateTimeElement
    {
        public NullableDateElement (string caption, DateTime? date, string nullButtonCaption, string setButtonCaption) : base (caption, date, nullButtonCaption, setButtonCaption)
        {
            initDateOnlyPicker ();
        }

        public NullableDateElement (string caption, DateTime? date, string nullButtonCaption) : base(caption, date, nullButtonCaption)
        {
            initDateOnlyPicker ();
        }

        public NullableDateElement (string caption, DateTime? date) : base(caption, date)
        {
            initDateOnlyPicker ();
        }


        void initDateOnlyPicker()
        {
            this.fmt.DateStyle = NSDateFormatterStyle.Medium;
        }

        public override string FormatDate (DateTime? dt)
        {
            if (dt.HasValue)
                return this.fmt.ToString (dt);
            else
                return base.NullButtonCaption;
        }
        public override UIDatePicker CreatePicker ()
        {
            UIDatePicker uIDatePicker = base.CreatePicker ();
            uIDatePicker.Mode = UIDatePickerMode.Date;
            return uIDatePicker;
        }
    }
}

@Miguel,请考虑将此添加到 MonoTouch.Dialog,因为对于空日期/日期时间存在非常合理的业务需求,并且此解决方案似乎可以解决问题。我的代码需要稍微清理一下,但这可行。

关于c# - MonoTouch.Dialog - 接受空值作为输入的 DateElement,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10199084/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap