沒有明顯的方法來做到這一點。 TimePickerFlyoutPresenter
中有一個關閉按鈕,但只能訪問這些是不夠的。您還需要管理鍵盤快捷鍵,如[ESC]鍵。
你讓我有點好奇如何破解它,所以這是我到目前爲止所提出的。這是一個TimePickerDismissal
類,我真的很快被砍了。這個想法是,如果TimePicker
在彈出一個彈出窗口並且沒有報告時間改變後獲得焦點,那麼它被認爲是解僱。
public class TimePickerDismissal
{
private bool _active;
private bool _timeChanged;
public event EventHandler Dismissed;
public TimePickerDismissal(TimePicker timer)
{
timer.GotFocus += OnTimeGotFocus;
timer.LostFocus += OnTimeLostFocus;
timer.TimeChanged += OnTimeChanged;
}
private void OnTimeGotFocus(object sender, RoutedEventArgs e)
{
if (!_active)
{
return;
}
_active = false;
if (!_timeChanged)
{
Dismissed?.Invoke(this, EventArgs.Empty);
}
_timeChanged = false;
}
private void OnTimeLostFocus(object sender, RoutedEventArgs e)
{
var selector = FocusManager.GetFocusedElement() as LoopingSelector;
if (selector == null)
{
return;
}
_active = true;
}
private void OnTimeChanged(object sender, TimePickerValueChangedEventArgs e)
{
_timeChanged = true;
}
}
下面是如何使用它:
public sealed partial class MainPage
{
public MainPage()
{
InitializeComponent();
var dismissal = new TimePickerDismissal(MyTimePicker);
dismissal.Dismissed += OnTimerDismissed;
}
private void OnTimerDismissed(object sender, EventArgs e)
{
Debug.WriteLine("TimePicker dismissed!");
}
}
把那一個鏡頭。請讓我知道這對你有沒有用。你可以把它變成一個實際上的行爲......