Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Xamarin.Forms中的可空时间选择器_C#_Xamarin_Xamarin.forms_Nullable_Timepicker - Fatal编程技术网

C# Xamarin.Forms中的可空时间选择器

C# Xamarin.Forms中的可空时间选择器,c#,xamarin,xamarin.forms,nullable,timepicker,C#,Xamarin,Xamarin.forms,Nullable,Timepicker,我正在使用TimePicker在我的应用程序中显示时间。当时间已设置时,它将正确显示,但当时间未设置时,它将显示默认的12:00 AM时间。所以我只想在未设置时间时显示null值。 是否可以在Xamarin表单中将null值设置为TimePicker /// <summary> /// DatePicker der null Werte erlaubt /// </summary> public class CustomDatePicker : DatePicker {

我正在使用
TimePicker
在我的应用程序中显示时间。当时间已设置时,它将正确显示,但当时间未设置时,它将显示默认的12:00 AM时间。所以我只想在未设置时间时显示
null
值。 是否可以在Xamarin表单中将
null
值设置为
TimePicker

/// <summary>
/// DatePicker der null Werte erlaubt
/// </summary>
public class CustomDatePicker : DatePicker
{
    /// <summary>
    /// PropertyName für die <c>NullableDate</c> Property
    /// </summary>
    public const string NullableDatePropertyName = "NullableDate";
    /// <summary>
    /// Die BinableProperty
    /// </summary>
    public static readonly BindableProperty NullableDateProperty = BindableProperty.Create<CustomDatePicker, DateTime?>(i => i.NullableDate, null, BindingMode.TwoWay, null, NullableDateChanged);
    /// <summary>
    /// Datumswert welches null Werte akzeptiert
    /// </summary>
    public DateTime? NullableDate
    {
        get
        {
            return (DateTime?)this.GetValue(NullableDateProperty);
        }
        set
        {
            this.SetValue(NullableDateProperty, value);
        }
    }
    /// <summary>
    /// Der Name der <c>NullText</c> Property
    /// </summary>
    public const string NullTextPropertyName = "NullText";
    /// <summary>
    /// Die BindableProperty
    /// </summary>
    public static readonly BindableProperty NullTextProperty = BindableProperty.Create<CustomDatePicker, string>(i => i.NullText, default(string), BindingMode.TwoWay);
    /// <summary>
    /// Der Text der angezeigt wird wenn <c>NullableDate</c> keinen Wert hat
    /// </summary>
    public string NullText
    {
        get
        {
            return (string)this.GetValue(NullTextProperty);
        }
        set
        {
            this.SetValue(NullTextProperty, value);
        }
    }
    /// <summary>
    /// Der Name der <c>DisplayBorder</c> Property
    /// </summary>
    public const string DisplayBorderPropertyName = "DisplayBorder";
    /// <summary>
    /// Die BindableProperty
    /// </summary>
    public static readonly BindableProperty DisplayBorderProperty = BindableProperty.Create<CustomDatePicker, bool>(i => i.DisplayBorder, default(bool), BindingMode.TwoWay);
    /// <summary>
    /// Gibt an ob eine Umrandung angezeigt werden soll oder nicht
    /// </summary>
    public bool DisplayBorder
    {
        get
        {
            return (bool)this.GetValue(DisplayBorderProperty);
        }
        set
        {
            this.SetValue(DisplayBorderProperty, value);
        }
    }

    /// <summary>
    /// Erstellt eine neue Instanz von <c>CustomDatePicker</c>
    /// </summary>
    public CustomDatePicker()
    {
        this.DateSelected += CustomDatePicker_DateSelected;

        this.Format = "dd.MM.yyyy";
    }
    /// <summary>
    /// Wird gefeuert wenn ein neues Datum selektiert wurde
    /// </summary>
    /// <param name="sender">Der Sender</param>
    /// <param name="e">Event Argumente</param>
    void CustomDatePicker_DateSelected(object sender, DateChangedEventArgs e)
    {
        this.NullableDate = new DateTime(
            e.NewDate.Year, 
            e.NewDate.Month, 
            e.NewDate.Day, 
            this.NullableDate.HasValue ? this.NullableDate.Value.Hour : 0,
            this.NullableDate.HasValue ? this.NullableDate.Value.Minute : 0,
            this.NullableDate.HasValue ? this.NullableDate.Value.Second : 0);
    }

    /// <summary>
    /// Gefeuert wenn sich <c>NullableDate</c> ändert
    /// </summary>
    /// <param name="obj">Der Sender</param>
    /// <param name="oldValue">Der alte Wert</param>
    /// <param name="newValue">Der neue Wert</param>
    private static void NullableDateChanged(BindableObject obj, DateTime? oldValue, DateTime? newValue)
    {
        var customDatePicker = obj as CustomDatePicker;

        if (customDatePicker != null)
        {
            if (newValue.HasValue)
            {
                customDatePicker.Date = newValue.Value;
            }
        }
    }
}
//
///空Werte erlaubt的日期选择器
/// 
公共类CustomDatePicker:DatePicker
{
/// 
///PropertyName für die NullableDate属性
/// 
public const字符串NullableDatePropertyName=“NullableDate”;
/// 
///模具二进制属性
/// 
public static readonly BindableProperty NullableDateProperty=BindableProperty.Create(i=>i.NullableDate,null,BindingMode.TwoWay,null,NullableDateChanged);
/// 
///Datumswert welches空值为akzeptiert
/// 
公共日期时间?NullableDate
{
得到
{
return(DateTime?)this.GetValue(NullableDateProperty);
}
设置
{
this.SetValue(NullableDateProperty,value);
}
}
/// 
///Der Name Der NullText属性
/// 
公用常量字符串NullTextPropertyName=“NullText”;
/// 
///模具可装订性
/// 
公共静态只读BindableProperty NullTextProperty=BindableProperty.Create(i=>i.NullText,默认值(字符串),BindingMode.TwoWay);
/// 
///文本中的所有内容均为空日期
/// 
公共字符串空文本
{
得到
{
返回(字符串)this.GetValue(NullTextProperty);
}
设置
{
this.SetValue(NullTextProperty,value);
}
}
/// 
///Der Name Der DisplayBorder属性
/// 
public const string DisplayBorderPropertyName=“DisplayBorder”;
/// 
///模具可装订性
/// 
公共静态只读BindableProperty DisplayBorderProperty=BindableProperty.Create(i=>i.DisplayBorder,默认值(bool),BindingMode.TwoWay);
/// 
///我的工作是独立完成的
/// 
公共边界
{
得到
{
返回(bool)this.GetValue(DisplayBorderProperty);
}
设置
{
此.SetValue(DisplayBorderProperty,value);
}
}
/// 
///客户数据采集器的新实例
/// 
公共CustomDatePicker()
{
this.DateSelected+=CustomDatePicker\u DateSelected;
this.Format=“dd.MM.yyyy”;
}
/// 
///我们将在新的基准面上进行测量
/// 
///发信人
///事件辩论
void CustomDatePicker\u DateSelected(对象发送方,DateChangedEventArgs e)
{
this.NullableDate=新的日期时间(
e、 新年,
e、 新日期,月份,
e、 NewDate,Day,
this.NullableDate.HasValue?this.NullableDate.Value.Hour:0,
this.NullableDate.HasValue?this.NullableDate.Value.Minute:0,
this.NullableDate.HasValue?this.NullableDate.Value.Second:0);
}
/// 
///格弗厄特·韦恩(Gefeuert wenn sich NullableDateändert)
/// 
///发信人
///阿尔特沃特酒店
///德尔纽维特酒店
私有静态无效NullableDateChanged(BindableObject obj、DateTime?oldValue、DateTime?newValue)
{
var customDatePicker=obj作为customDatePicker;
if(customDatePicker!=null)
{
if(newValue.HasValue)
{
customDatePicker.Date=newValue.Value;
}
}
}
}

您可以使用
计时器
标签创建一个自定义
视图
。如果未选择时间,则将显示“hh:mm”,否则时间将显示在标签上。要显示时间选择器对话框,有一个
tappesturerecognizer
,当点击标签时,它会将焦点设置为时间选择器

public class NullableTimePicker : ContentView
{
    private const string NullTimeLabel = "hh : mm";
    private readonly TimePicker _timePicker;
    private readonly Label _label;                

    public static readonly BindableProperty TimeProperty = BindableProperty.Create<NullableTimePicker, TimeSpan?>(t => t.Time, null, BindingMode.TwoWay, propertyChanged: OnTimeChanged);

    public NullableTimePicker()
    {
         _label = new Label
         {
              Text = NullTimeLabel,
              HorizontalOptions = LayoutOptions.FillAndExpand,
              VerticalOptions = LayoutOptions.Fill,
              HorizontalTextAlignment = TextAlignment.Start,
              VerticalTextAlignment = TextAlignment.Center,
              TextColor = Color.Black,
              FontSize = Device.GetNamedSize(NamedSize.Medium, typeof(Label))
         };

         _timePicker = new TimePicker
         {
              IsVisible = false,
              HorizontalOptions = LayoutOptions.FillAndExpand,
              VerticalOptions = LayoutOptions.Fill
         };

         Content = new StackLayout
         {
              Children =
              {
                   _label,
                   _timePicker
              },
              Padding = 0,
              Spacing = 0,
              Margin = 0
         };

         Padding = 0;
         Margin = 0;

         var tapGestureRecognizer = new TapGestureRecognizer();
         tapGestureRecognizer.Tapped += TapGestureRecognizer_Tapped;
         GestureRecognizers.Add(tapGestureRecognizer);

         _timePicker.PropertyChanged += timePicker_PropertyChanged;

         PropertyChanged += NullableTimePicker_PropertyChanged;
   }

  private void NullableTimePicker_PropertyChanged(object sender, PropertyChangedEventArgs e)
  {
         if (_label != null && e.PropertyName == IsEnabledProperty.PropertyName)
         {
               _label.TextColor = IsEnabled ? Color.Black : Color.Gray;
         }
  }

  private void timePicker_PropertyChanged(object sender, PropertyChangedEventArgs e)
  {
         if (e.PropertyName == TimePicker.TimeProperty.PropertyName && _timePicker != null)
         {
                    Time = _timePicker.Time;
         }
   }

   private void TapGestureRecognizer_Tapped(object sender, EventArgs e)
   {
          if (IsEnabled == false)
          {
               return;
          }

          if (_timePicker.IsFocused)
          {
              _timePicker.Unfocus();
          }

          _timePicker.Focus();
     }

     private static void OnTimeChanged(BindableObject bindable, TimeSpan? oldvalue, TimeSpan? newvalue)
     {
           var nullableTimePicker = bindable as NullableTimePicker;
           if (nullableTimePicker != null && oldvalue != newvalue)
           {
               nullableTimePicker.Time = newvalue;
           }
     }

     public TimeSpan? Time
     {
          get
          {
               return GetValue(TimeProperty) as TimeSpan?;
          }
          set
          {
               SetValue(TimeProperty, value);

               if (value.HasValue && _timePicker.Time != value)
               {
                   _timePicker.Time = value.Value;
               }
               SetLabelText(value);
           }
      }

      private void SetLabelText(TimeSpan? value)
      {
           _label.Text = value.HasValue ? ConvertTimeSpanToString(value.Value) : NullTimeLabel;
      }          
}
公共类NullableTimePicker:ContentView
{
私有常量字符串NullTimeLabel=“hh:mm”;
私有只读时间选择器_TimePicker;
专用只读标签_标签;
public static readonly BindableProperty TimeProperty=BindableProperty.Create(t=>t.Time,null,BindingMode.TwoWay,propertyChanged:OnTimeChanged);
公共NullableTimePicker()
{
_标签=新标签
{
Text=空时间标签,
HorizontalOptions=LayoutOptions.FillAndExpand,
垂直选项=布局选项。填充,
HorizontalTextAlignment=TextAlignment.Start,
VerticalTextAlignment=TextAlignment.Center,
TextColor=Color.Black,
FontSize=Device.GetNamedSize(NamedSize.Medium,typeof(Label))
};
_计时器选择器=新计时器选择器
{
IsVisible=false,
HorizontalOptions=LayoutOptions.FillAndExpand,
垂直选项=布局选项。填充
};
内容=新的堆栈布局
{
孩子们=
{
_标签,
_计时器选择器
},
填充=0,
间距=0,
保证金=0
};
填充=0;
保证金=0;
var tapGestureRecognizer=新的tapGestureRecognizer();
tapGestureRecognizer.Tapped+=tapGestureRecognizer_Tapped;
添加(点击手势识别器);
_timePicker.PropertyChanged+=timePicker\u PropertyChanged;
PropertyChanged+=NullableTimePicker\u PropertyChanged;
}
私有void NullableTimePicker\u PropertyChanged(对象发送方,PropertyChangedEventArgs e)
{
if(_label!=null&&e.PropertyName==IsEnabledProperty.PropertyName)
{
_label.TextColor=IsEnabled?颜色。黑色:颜色。灰色;
}
}
私有无效时间选择器\u属性已更改(对象se