我发现DatePicker缺少一个占位符。在未指定日期的情况下决定显示标签而不是DatePicker。但是,当单击标签时,我需要打开DatePicker,但它不起作用
.xaml文件:
<controls:DatePickerWithoutBottomLine
x:Name="datePicker"
IsVisible="{Binding DisplayWeddingDate, Mode=TwoWay}"
Date="{Binding WeddingDate, Mode=TwoWay}" />
<Label
IsVisible="{Binding NotDisplayWeddingDate}"
Text="some text">
<Label.GestureRecognizers>
<TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped" />
</Label.GestureRecognizers>
</Label>
.xaml.文件夹:
private void TapGestureRecognizer_Tapped(object sender, TappedEventArgs e)
{
var label = (Label)sender;
var datePicker = this.FindByName<DatePicker>("datePicker");
label.IsVisible = false;
datePicker.IsVisible = true;
datePicker.Focus();
}
视图模型.cs:
public partial class HomePageViewModel : BaseViewModel
{
[ObservableProperty]
private DateTime? weddingDate;
[ObservableProperty]
private bool displayWeddingDate;
[ObservableProperty]
private bool notDisplayWeddingDate;
private readonly PlanningService planningService;
[ObservableProperty]
private bool isRefreshing;
public HomePageViewModel(PlanningService planningService)
{
this.planningService = planningService;
InitAsync();
}
public override async void InitAsync()
{
await RefreshAsync();
}
[RelayCommand]
private async Task RefreshAsync()
{
WeddingDate = await planningService.GetWeddingDateAsync();
DisplayWeddingDate = WeddingDate.HasValue;
NotDisplayWeddingDate = !DisplayWeddingDate;
IsRefreshing = false;
}
}
1条答案
按热度按时间zc0qhyus1#
如果日期选取器的IsVisible为false,则它不是可视化树的一部分。那里没有要聚焦的日期选取器。
试着使用这个. dispatcher.dispatch来延迟焦点,给予它一点时间来呈现。