【问题标题】:Xamarin.Forms detect Custom ContentView click outside eventXamarin.Forms 检测自定义 ContentView 单击外部事件
【发布时间】:2020-09-24 09:09:30
【问题描述】:

我有一个 CustomCalendar 元素,它是通过扩展 ContentView 并在另一个 ContentPage 中使用此自定义视图而创建的。我尝试使用 Unfocused 事件来检测外部点击。但问题是它没有触发事件处理程序。您能否建议我以更好的方式检测点击之外的元素。

我在页面中以这种方式使用我的自定义视图,其中包含 Unfocused EventToCommandBehavior

<views:CustomCalendar x:Name="cal">
       <views:Calendar.Behaviors>
             <prism:EventToCommandBehavior 
                           EventName="Unfocused"
                           Command="{Binding UnfocusedCalandar}"/>
       </views:Calendar.Behaviors>
</views:Calendar>

【问题讨论】:

    标签: xamarin mvvm xamarin.forms prism


    【解决方案1】:

    Unfocused 事件在 VisualElement 失去焦点时引发,它只适用于能够接收焦点的元素,不幸的是 ContentView 无法接收焦点,所以 focusedUnfocused 事件永远不会触发内容视图。

    可以接收焦点的元素:Entry、Editor、Picker等..


    作为一个临时解决方法,你可以将 ContentView 包装到 StackLayout 中,在 ContentView 和 parenet 布局上设置点击手势,它自己的点击手势会阻止父视图的手势。

    <StackLayout BackgroundColor="Red" >
            <ContentView HeightRequest="100" WidthRequest="100" BackgroundColor="Blue" >
                <ContentView.GestureRecognizers>
                    <TapGestureRecognizer Tapped="ContentViewTap"/>
                </ContentView.GestureRecognizers>
            </ContentView>
    
    
            <CollectionView BackgroundColor="Gray" Focused="CollectionView_Focused" >
                <CollectionView.ItemsSource>
                    <x:Array Type="{x:Type x:String}">
                        <x:String>Baboon</x:String>
                        <x:String>Capuchin Monkey</x:String>
                        <x:String>Blue Monkey</x:String>
                        <x:String>Squirrel Monkey</x:String>
                        <x:String>Golden Lion Tamarin</x:String>
                        <x:String>Howler Monkey</x:String>
                        <x:String>Japanese Macaque</x:String>
                    </x:Array>
                </CollectionView.ItemsSource>
                <CollectionView.ItemTemplate>
                    <DataTemplate>
                        <Grid Padding="10">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto" />
                                <RowDefinition Height="Auto" />
                            </Grid.RowDefinitions>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto" />
                                <ColumnDefinition Width="Auto" />
                            </Grid.ColumnDefinitions>
                            <Image Grid.RowSpan="2"
                           Source="dog.png"
                           Aspect="AspectFill"
                           HeightRequest="60"
                           WidthRequest="60" />
                            <Label Grid.Column="1"
                           Text="{Binding }"
                           FontAttributes="Bold" />
                            <Label Grid.Row="1"
                           Grid.Column="1"
                           Text="{Binding }"
                           FontAttributes="Italic"
                           VerticalOptions="End" />
    
                            <Grid.GestureRecognizers>
                                <TapGestureRecognizer Tapped="StackLayoutTap" />
                            </Grid.GestureRecognizers>
                        </Grid>
    
    
                    </DataTemplate>
                </CollectionView.ItemTemplate>
    
    
            </CollectionView>
    
            <StackLayout.GestureRecognizers>
                <TapGestureRecognizer Tapped="StackLayoutTap" />
            </StackLayout.GestureRecognizers>
        </StackLayout>
    

    【讨论】:

    • 我试过这种方式,但它只有在堆栈布局被准确点击时才会触发。当我们单击堆栈布局中的其他元素时,它不会触发。
    • StackLayoutTap 当我们点击堆栈布局中的所有元素时触发,它在我这边工作正常。
    • 在我的 XAML 中,StackLayout 中有一个 CollectionView,因此当我单击 CollectionView 中的任何项目时,它都不会触发。
    • CollectionView.ItemTemplate内的元素上添加Tap手势,并将事件分配给StackLayoutTap ,查看我的更新。
    • 是的,这是一个很好的方法,但这里的问题是,如果有不同类型的布局,我们需要将点击手势添加到所有这些布局中。
    猜你喜欢
    • 1970-01-01
    • 2013-02-18
    • 1970-01-01
    • 2021-02-09
    • 1970-01-01
    • 1970-01-01
    • 2020-11-22
    • 2020-08-27
    • 1970-01-01
    相关资源
    最近更新 更多