通过点击 xamarin.forms 中列表中的图像来聚焦时间选择器

Focus timepicker by tapping on image in a list in xamarin.forms

我有一个列表视图,它由带有时间选择器的堆栈布局和图像组成。我想要的是在点击图像时 timePicker 应该聚焦。如果这些控件不在列表视图中,可以通过 X:Name 访问它们,但是当它们在列表视图中时我如何才能聚焦。

首先你需要创建一个自定义的ViewCell

CustomCell.xaml

<ViewCell.View>
        <StackLayout>
            <Button Text="ImagTest" Clicked="Button_Clicked"/>
            <Picker x:Name="pickerTest"
        </StackLayout>
    </ViewCell.View>

然后,在CustomCell.xaml.cs

public partial class CustomCell : ViewCell
    {
        public CustomCell ()
        {
            InitializeComponent ();
        }

        private void Button_Clicked(object sender, EventArgs e)
        {
            pickerTest.Focus();
        }
    }

在您的列表视图页面中定义命名空间

 xmlns:local="clr-namespace:YourProject;assembly=YourProject"

在您的列表视图中

<ListView HasUnevenRows="True" ItemSelected="ListView_ItemSelected" x:Name="listView">
        <ListView.ItemTemplate>
            <DataTemplate>
                <local:CustomCell/>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>