Detect a touch outside of a custom pop up inside ContentView

SiddySiddy Member ✭✭

I want to implement closing of a custom pop up view which is inside my content view. I followed e.g. given in the following link for implementing a custom pop up
https://dzone.com/articles/xamarinforms-custom-popup

Following is my code snippet.

<AbsoluteLayout Padding="0" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
            <StackLayout AbsoluteLayout.LayoutBounds="0, 0, 1, 1" AbsoluteLayout.LayoutFlags="All">                

                <ListView x:Name="listview"
                  ItemSelected="listview_ItemSelected">
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <TextCell Text="{Binding FirstName}"
                              Detail="{Binding CreatedDate}" />
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>

            </StackLayout>

                <ContentView x:Name="popupListView" BackgroundColor="#C0808080" Padding="10, 0" IsVisible="False" AbsoluteLayout.LayoutBounds="0, 0, 1, 1" AbsoluteLayout.LayoutFlags="All"
                             >
                    <StackLayout VerticalOptions="Center" HorizontalOptions="Center">
                    <StackLayout Orientation="Vertical" HeightRequest="200" WidthRequest="300" BackgroundColor="White">
                            <ListView x:Name="sampleList" ItemTapped="sampleList_ItemTapped">
                            </ListView>
                        <Button x:Name="btnDismiss" Text="Dismiss" Clicked="btnDismiss_Clicked" Margin="10" />
                    </StackLayout>
                    </StackLayout>
                </ContentView>                            
        </AbsoluteLayout>

I have put a button to dismiss the pop up, but want to implement close if user click outside of content view as well. I have done some looking online for the same, but most implement popupwindow, or check for region on tap.

Any help or suggestions to achieve the required functionality.

Thanks In Advance.

Tagged:

Answers

  • ColeXColeX Member, Xamarin Team Xamurai

    You'll need to add TapGestureRecognizer inside ContentView and bind Button Click event to it .

     <ContentView x:Name="popupListView" 
                     BackgroundColor="Red"
                     Padding="10, 0"
                     IsVisible="True" 
                     AbsoluteLayout.LayoutBounds="0, 0, 1, 1" 
                     AbsoluteLayout.LayoutFlags="All">
    
            <ContentView.GestureRecognizers>
                <TapGestureRecognizer Tapped="btnDismiss_Clicked"/>
            </ContentView.GestureRecognizers>
           //xxxx
    
Sign In or Register to comment.