Share via

Xamarin Forms: DisplayAlert background tap firing the cancel event

Sreejith Sree 1,251 Reputation points
2021-04-19T11:47:47.053+00:00

I am using a DisplayAlert like below in my project.

var answer = await DisplayAlert("Alert", "You are invited to join a group, would you like to accept it or not?", "Accept", "Reject");
if (answer)
{
//accept invitation
}
else
{
//reject invitation
}

Accept and Reject options are working fine. My problem is Reject option is executing when tapping on the background or device back arrow. Is it has a simple solution other than implementing Rg.Plugins.Popup?

Developer technologies | .NET | Xamarin
0 comments No comments

Answer accepted by question author

  1. JarvanZhang 23,971 Reputation points
    2021-04-19T13:37:18.923+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    Xamarin.Forms doesn't provide the property to prevent closing the alert when clicking the background like 'Rg.Plugins.Popup' plugin. For this requirement, you could use the popup plugin to display the popup and set CloseWhenBackgroundIsClicked to true.

       <pages:PopupPage   
               ...  
               CloseWhenBackgroundIsClicked="False">  
           ...  
       </pages:PopupPage>  
    

    Or customize a popup view and display it in the AbsoluteLayout. Then change the visibility of the view when clicking a button.

       <AbsoluteLayout>  
           <ContentView x:Name="popupView" BackgroundColor="#C0808080" Padding="10, 0" IsVisible="false" AbsoluteLayout.LayoutBounds="0, 0, 1, 1" AbsoluteLayout.LayoutFlags="All">  
               <ContentView.Content>  
                   <StackLayout VerticalOptions="Center" HorizontalOptions="Center">  
                       <StackLayout Orientation="Vertical" HeightRequest="150" WidthRequest="200" BackgroundColor="White">  
         
                           <Label x:Name="lblLoadingText" TextColor="Black" VerticalOptions="Center" HorizontalOptions="Center" VerticalTextAlignment="Center" Text="Loading..."></Label>  
                       </StackLayout>  
                   </StackLayout>  
               </ContentView.Content>  
           </ContentView>  
         
           <StackLayout AbsoluteLayout.LayoutFlags="All" AbsoluteLayout.LayoutBounds="0,0,1,1">  
               <!--the content-->  
           </StackLayout>  
       </AbsoluteLayout>  
    

    Best Regards,

    Jarvan Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Sreejith Sree 1,251 Reputation points
    2021-04-19T14:04:22.617+00:00

    I quickly "solved it" with a workaround like this (using DisplayActionSheet):

    bool isActionSelected = false;
    while(!isActionSelected)
    {
        string action = await DisplayActionSheet ("You are invited to join a group, would you like to accept it or not?", null, null, "Accept", "Reject");
        if (action == "Accept")
        {
            isActionSelected = true;
            //do stuff         
        }
        else if (action == "Reject")
        {
            isActionSelected = true;
            //do stuff         
        }
        else
        {
            isActionSelected = false;
        }
    }
    

    This is not suggested, unless you are in a hurry.

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.