xamarin 聚焦于DatePicker不起作用MAUI

1u4esq0p  于 2023-01-28  发布在  其他
关注(0)|答案(1)|浏览(190)

我发现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;
    }
}
zc0qhyus

zc0qhyus1#

如果日期选取器的IsVisible为false,则它不是可视化树的一部分。那里没有要聚焦的日期选取器。

datePicker.IsVisible = true;
datePicker.Focus();

试着使用这个. dispatcher.dispatch来延迟焦点,给予它一点时间来呈现。

datePicker.IsVisible = true;
        this.Dispatcher.Dispatch(() =>
        {
            datePicker.Focus();
        });

相关问题