0
我有一個日期選取器在Xamarin表格頁面。在日期選擇器中默認設置今天的日期。是否有任何選項可以刪除該日期,以便當用戶不從日期選擇器中選擇日期時,我可以發送空值。如何刪除日期選取器中的日期(Xamarin表格)
我有一個日期選取器在Xamarin表格頁面。在日期選擇器中默認設置今天的日期。是否有任何選項可以刪除該日期,以便當用戶不從日期選擇器中選擇日期時,我可以發送空值。如何刪除日期選取器中的日期(Xamarin表格)
我們需要寫CustomRederer爲的DatePicker
在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"
};
我想你可以通過它的值只設置爲'null'做 –
@GeraldVersluis這是不可能的,因爲使用默認的DatePicker,DateTime不能有空值。您可以添加複選框(例如「啓用日期」)。如果未選中複選框,則使用空值。如果它被選中,那麼使用'DatePicker'包含的任何值......或者你可以編寫自己的'DatePicker':https://forums.xamarin.com/discussion/20028/datepicker-possible-to-bind-to-可空日期值 –
使用DateTime.MinValue而不是null –