Udd*_*rne 5 c# nullable timepicker xamarin xamarin.forms
我用于TimePicker在我的应用程序中显示时间。当时间已设置时,它会正确显示,但当时间未设置时,则显示默认 12 : 00 AM 时间。所以我只想null在未设置时间时显示值。是否可以在 Xamarin Forms 中设置nullable值TimePicker?
我用这个
\n\n/// <summary>\n/// DatePicker der null Werte erlaubt\n/// </summary>\npublic class CustomDatePicker : DatePicker\n{\n /// <summary>\n /// PropertyName f\xc3\xbcr die <c>NullableDate</c> Property\n /// </summary>\n public const string NullableDatePropertyName = "NullableDate";\n /// <summary>\n /// Die BinableProperty\n /// </summary>\n public static readonly BindableProperty NullableDateProperty = BindableProperty.Create<CustomDatePicker, DateTime?>(i => i.NullableDate, null, BindingMode.TwoWay, null, NullableDateChanged);\n /// <summary>\n /// Datumswert welches null Werte akzeptiert\n /// </summary>\n public DateTime? NullableDate\n {\n get\n {\n return (DateTime?)this.GetValue(NullableDateProperty);\n }\n set\n {\n this.SetValue(NullableDateProperty, value);\n }\n }\n /// <summary>\n /// Der Name der <c>NullText</c> Property\n /// </summary>\n public const string NullTextPropertyName = "NullText";\n /// <summary>\n /// Die BindableProperty\n /// </summary>\n public static readonly BindableProperty NullTextProperty = BindableProperty.Create<CustomDatePicker, string>(i => i.NullText, default(string), BindingMode.TwoWay);\n /// <summary>\n /// Der Text der angezeigt wird wenn <c>NullableDate</c> keinen Wert hat\n /// </summary>\n public string NullText\n {\n get\n {\n return (string)this.GetValue(NullTextProperty);\n }\n set\n {\n this.SetValue(NullTextProperty, value);\n }\n }\n /// <summary>\n /// Der Name der <c>DisplayBorder</c> Property\n /// </summary>\n public const string DisplayBorderPropertyName = "DisplayBorder";\n /// <summary>\n /// Die BindableProperty\n /// </summary>\n public static readonly BindableProperty DisplayBorderProperty = BindableProperty.Create<CustomDatePicker, bool>(i => i.DisplayBorder, default(bool), BindingMode.TwoWay);\n /// <summary>\n /// Gibt an ob eine Umrandung angezeigt werden soll oder nicht\n /// </summary>\n public bool DisplayBorder\n {\n get\n {\n return (bool)this.GetValue(DisplayBorderProperty);\n }\n set\n {\n this.SetValue(DisplayBorderProperty, value);\n }\n }\n\n /// <summary>\n /// Erstellt eine neue Instanz von <c>CustomDatePicker</c>\n /// </summary>\n public CustomDatePicker()\n {\n this.DateSelected += CustomDatePicker_DateSelected;\n\n this.Format = "dd.MM.yyyy";\n }\n /// <summary>\n /// Wird gefeuert wenn ein neues Datum selektiert wurde\n /// </summary>\n /// <param name="sender">Der Sender</param>\n /// <param name="e">Event Argumente</param>\n void CustomDatePicker_DateSelected(object sender, DateChangedEventArgs e)\n {\n this.NullableDate = new DateTime(\n e.NewDate.Year, \n e.NewDate.Month, \n e.NewDate.Day, \n this.NullableDate.HasValue ? this.NullableDate.Value.Hour : 0,\n this.NullableDate.HasValue ? this.NullableDate.Value.Minute : 0,\n this.NullableDate.HasValue ? this.NullableDate.Value.Second : 0);\n }\n\n /// <summary>\n /// Gefeuert wenn sich <c>NullableDate</c> \xc3\xa4ndert\n /// </summary>\n /// <param name="obj">Der Sender</param>\n /// <param name="oldValue">Der alte Wert</param>\n /// <param name="newValue">Der neue Wert</param>\n private static void NullableDateChanged(BindableObject obj, DateTime? oldValue, DateTime? newValue)\n {\n var customDatePicker = obj as CustomDatePicker;\n\n if (customDatePicker != null)\n {\n if (newValue.HasValue)\n {\n customDatePicker.Date = newValue.Value;\n }\n }\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
4136 次 |
| 最近记录: |