如何删除日期选择器中的日期(Xamarin表单)

时间:2017-06-13 11:27:44

标签: c# xamarin xamarin.forms

我在Xamarin表单页面中有一个日期选择器。在日期选择器今天默认设置的日期。是否有任何选项可以删除该日期,因此当用户不从日期选择器中选择日期时,我可以发送空值。

2 个答案:

答案 0 :(得分:2)

我们需要为DatePicker编写CustomRederer

在PCL中:

 using System;
 using Xamarin.Forms;
 namespace IncAlert
   {
    public class CustomDatePicker : DatePicker
      {
        public CustomDatePicker()
         {
        TextColor = Color.FromHex("#282828");
         }
      public static readonly BindableProperty EnterTextProperty = BindableProperty.Create(propertyName: "EnterText", returnType: typeof(string), declaringType: typeof(CustomDatePicker), defaultValue: default(string));
    public string EnterText { get; set; }

    public static readonly BindableProperty CustomFontFamilyProperty = BindableProperty.Create(propertyName: "CustomFontFamily", returnType: typeof(string), declaringType: typeof(CustomDatePicker), defaultValue: default(string));
    public string CustomFontFamily { get; set; }

    public static readonly BindableProperty CustomFontSizeProperty = BindableProperty.Create(propertyName: "CustomFontSize", returnType: typeof(float), declaringType: typeof(CustomDatePicker), defaultValue: default(float));
    public float CustomFontSize { get; set; }

   }
}

<强> Xamarin.Android:

using System;
using Android.App;
using Android.Content.Res;
using Android.Graphics.Drawables;
using Android.Text;
using Android.Util;
using IncAlert;
using IncAlert.Droid;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
using Graphicss = Android.Graphics;

  [assembly: ExportRenderer(typeof(CustomDatePicker), 
  typeof(CustomDatePickerRender))]
  namespace IncAlert.Droid
  {
   public class CustomDatePickerRender : DatePickerRenderer
    {
    public CustomDatePickerRender(){}

    protected override void OnElementChanged(ElementChangedEventArgs<DatePicker> e)
    {
        base.OnElementChanged(e);

        try
        {
            CustomDatePicker element = Element as CustomDatePicker;
            if (e.NewElement != null)
            {
                element = Element as CustomDatePicker;
            }
            else
            {
                element = e.OldElement as CustomDatePicker;
            }

            if (Control != null)
            {
                //var element = Element as CustomDatePicker;
                GradientDrawable gd = new GradientDrawable();
                //gd.SetCornerRadius(45); // increase or decrease to changes the corner look
                gd.SetColor(global::Android.Graphics.Color.Transparent);
                //gd.SetStroke(2, global::Android.Graphics.Color.Gray);
                this.Control.SetBackgroundDrawable(gd);
                this.Control.SetRawInputType(InputTypes.TextFlagNoSuggestions);
                if (!string.IsNullOrWhiteSpace(element.EnterText))
                {
                    Control.Text = element.EnterText;
                }
                Control.SetHintTextColor(ColorStateList.ValueOf
   (global::Android.Graphics.Color.Black));//for placeholder
                if (element.CustomFontSize != 0.0)
                {
                    Control.SetTextSize(ComplexUnitType.Dip, element.CustomFontSize);
                    //Control.SetTextSize(Android.Util.ComplexUnitType.Dip, element.CustomFontSize);
                }

                if (element.CustomFontFamily == "Avenir65")
                {
                    Graphicss.Typeface font = Graphicss.Typeface.CreateFromAsset(Forms.Context.Assets, "AvenirLTStd-Medium.ttf");
                    Control.Typeface = font;
                }
                else if (element.CustomFontFamily == "Avenir45")
                {
                    Graphicss.Typeface font = Graphicss.Typeface.CreateFromAsset(Forms.Context.Assets, "AvenirLTStd-Book.ttf");
                    Control.Typeface = font;
                }
                else
                {
                }
            }
        }
        catch (Exception ex)
        {
            var msg = ex.Message;
        }
        //this.Control.InputType = InputTypes.TextVariationPassword;
    }

    protected override void OnElementPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
    {
        base.OnElementPropertyChanged(sender, e);
        try
        {
            CustomDatePicker element = Element as CustomDatePicker;
            if (Control != null)
            {
                //var element = Element as CustomDatePicker;
                GradientDrawable gd = new GradientDrawable();
                //gd.SetCornerRadius(45); // increase or decrease to changes the corner look
                gd.SetColor(global::Android.Graphics.Color.Transparent);
                //gd.SetStroke(2, global::Android.Graphics.Color.Gray);
                this.Control.SetBackgroundDrawable(gd);
                this.Control.SetRawInputType(InputTypes.TextFlagNoSuggestions);
                if (!string.IsNullOrWhiteSpace(element.EnterText))
                {
                    //Control.Text = element.EnterText;
                }
                Control.SetHintTextColor(ColorStateList.ValueOf(global::Android.Graphics.Color.Black));//for placeholder
                if (element.CustomFontSize != 0.0)
                {
                    Control.SetTextSize(ComplexUnitType.Dip, element.CustomFontSize);
                    //Control.SetTextSize(Android.Util.ComplexUnitType.Dip, element.CustomFontSize);
                }

                if (element.CustomFontFamily == "Avenir65")
                {
                    Graphicss.Typeface font = Graphicss.Typeface.CreateFromAsset(Forms.Context.Assets, "AvenirLTStd-Medium.ttf");
                    Control.Typeface = font;
                }
                else if (element.CustomFontFamily == "Avenir45")
                {
                    Graphicss.Typeface font = Graphicss.Typeface.CreateFromAsset(Forms.Context.Assets, "AvenirLTStd-Book.ttf");
                    Control.Typeface = font;
                }
                else
                {
                }
            }
        }
        catch (Exception ex)
        {
            var msg = ex.Message;
        }
        //this.Control.InputType = InputTypes.TextVariationPassword;
       }
   }
}

<强> Xamarin.iOS:

  using System;
  using IncAlert;
  using IncAlert.iOS;
  using UIKit;
  using Xamarin.Forms;
  using Xamarin.Forms.Platform.iOS;

   [assembly: ExportRenderer(typeof(CustomDatePicker), 
  typeof(CustomDatePickerRender))]
   namespace IncAlert.iOS
    {
     public class CustomDatePickerRender : DatePickerRenderer
       {
    public CustomDatePickerRender(){}

    protected override void OnElementChanged(ElementChangedEventArgs<DatePicker> e)
    {
        base.OnElementChanged(e);

        try
        {
            CustomDatePicker element = Element as CustomDatePicker;
            if (e.NewElement != null)
            {
                element = Element as CustomDatePicker;
            }
            else
            {
                element = e.OldElement as CustomDatePicker;
            }

            if (Control != null)
            {
                //var element = Element as CustomDatePicker;
                var textGiven = element.EnterText;
                Control.BorderStyle = UITextBorderStyle.None;
                Control.AdjustsFontSizeToFitWidth = true;
                Control.Layer.CornerRadius = 10;
                Control.ExclusiveTouch = true;
                if (!string.IsNullOrWhiteSpace(textGiven))
                {
                    Control.Text = textGiven;
                }
                Control.TextColor = UIColor.Black;
                if (element.CustomFontFamily == "Avenir65")
                {
                    Control.Font = UIFont.FromName("AvenirLTStd-Medium.ttf", 15f);
                }
                else if (element.CustomFontFamily == "Avenir45")
                {
                    Control.Font = UIFont.FromName("AvenirLTStd-Book.ttf", 15f);
                }
                else
                {
                }
                if (element.CustomFontSize != 0)
                {
                    UIFont font = Control.Font.WithSize(element.CustomFontSize);
                    Control.Font = font;
                }
                else
                {
                }
            }
        }
        catch (Exception ex)
        {
            var msg = ex.Message;
        }
    }
    protected override void OnElementPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
    {
        base.OnElementPropertyChanged(sender, e);
        try
        {
            CustomDatePicker element = Element as CustomDatePicker;

            if (Control != null)
            {
                //var element = Element as CustomDatePicker;
                var textGiven = element.EnterText;
                Control.BorderStyle = UITextBorderStyle.None;
                Control.AdjustsFontSizeToFitWidth = true;
                Control.Layer.CornerRadius = 10;
                Control.ExclusiveTouch = true;
                if (!string.IsNullOrWhiteSpace(textGiven))
                {
                    //Control.Text = textGiven;
                }
                Control.TextColor = UIColor.Black;
                if (element.CustomFontFamily == "Avenir65")
                {
                    Control.Font = UIFont.FromName("AvenirLTStd-Medium.ttf", 15f);
                }
                else if (element.CustomFontFamily == "Avenir45")
                {
                    Control.Font = UIFont.FromName("AvenirLTStd-Book.ttf", 15f);
                }
                else
                {
                }
                if (element.CustomFontSize != 0)
                {
                    UIFont font = 
    Control.Font.WithSize(element.CustomFontSize);
                    Control.Font = font;
                }
                else
                {
                }
            }
        }
        catch (Exception ex)
        {
            var msg = ex.Message;
        }
       }
   }
 }

在XAML中:

   <local:CustomDatePicker EnterText=" "/>

(OR)

在C#中:

     CustomDatePicker regDateEntry = new CustomDatePicker()
        {
            EnterText = " ",
            CustomFontSize =15,
            CustomFontFamily = "Avenir45"
        };

答案 1 :(得分:0)

我使用 App.xaml 中定义的两种不同样式解决了这个问题。一种样式是默认的 DatePicker 样式,另一种样式用于禁用日期选择器。禁用样式将 TextColor 设置为与控件的 BackgroundColor 相同。

我还有一个复选框来启用/禁用用户是否想要设置日期(取决于您的应用程序需要)。选中复选框时,我设置的是默认样式,取消选中时,我设置的是禁用样式。

结果如下:

检查:

DatePicker Enabled

未选中(这也有日期,但未向用户显示):

DatePicker Disabled