我已将自定义的TimePicker设置为具有24H格式,尤其是在iOS中。 但是我的问题是在Android Renderer中。 在我的页面中,我有两个TimePicker,当我更新其中一个的值时,我还必须将此值与其他选择器时间相加。
安卓渲染器
public class CustomTimePickerRenderer : ViewRenderer<TimePicker, Android.Widget.EditText>, TimePickerDialog.IOnTimeSetListener, IJavaObject, IDisposable
{
TimePickerDialog dialog;
public CustomTimePickerRenderer(Context context) : base(context)
{
}
Context CurrentContext => Plugin.CurrentActivity.CrossCurrentActivity.Current.Activity;
IElementController ElementController => Element as IElementController;
protected override void OnElementChanged(ElementChangedEventArgs<TimePicker> e)
{
base.OnElementChanged(e);
SetNativeControl(new Android.Widget.EditText(CurrentContext));
if (Control != null)
{
Control.Click += Control_Click;
Control.FocusChange += Control_FocusChange;
this.Control.KeyListener = null;
DateTime d = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
if (Element != null && !Element.Time.Equals(default(TimeSpan)))
Control.Text = Element.Time.ToString(@"hh\:mm");
else
Control.Text = d.ToString("HH:mm");
}
}
void Control_FocusChange(object sender, FocusChangeEventArgs e)
{
if (e.HasFocus)
{
ShowTimePicker();
ElementController.SetValueFromRenderer(VisualElement.IsFocusedPropertyKey, true);
}
else
{
ElementController.SetValueFromRenderer(VisualElement.IsFocusedPropertyKey, false);
}
}
void Control_Click(object sender, EventArgs e)
{
ShowTimePicker();
}
void ShowTimePicker()
{
if (dialog == null)
dialog = new TimePickerDialog(CurrentContext, this, Element.Time.Hours, Element.Time.Minutes, true);
dialog.UpdateTime(Element.Time.Hours, Element.Time.Minutes);
dialog.Show();
}
public void OnTimeSet(Android.Widget.TimePicker view, int hourOfDay, int minute)
{
var time = new TimeSpan(hourOfDay, minute, 0);
Element.SetValue(TimePicker.TimeProperty, time);
Control.Text = time.ToString(@"hh\:mm");
ClearFocus();
}
}
XAML
<local:CustomTimePicker x:Name="TempoAttesa" Unfocused="AggiornaDurata" Format="HH:mm"/>
<local:CustomTimePicker x:Name="Durata" Time="{Binding Durata, Mode=TwoWay}" Format="HH:mm"/>
C#
private void AggiornaDurata(object sender, EventArgs e)
{
Durata.Time += TempoAttesa.Time;
}
AggiornaDurata方法仅将一个TimePicker的时间与另一个TimePicker的时间相加。 Durata.Time值已更新,但未显示文本。
那么,我是否忘记了渲染器中的某些内容?