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