How to hide image button after it has been tapped in xamarin

Esteban Peralta 26 Reputation points
2021-09-12T19:51:58.377+00:00

I'm building a Xamarin app and in one of my pages I have an image that I use as a button, and I want to hide it after it's been tapped. How can this be achieved?

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,295 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,276 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Kyle Wang 5,531 Reputation points
    2021-09-13T01:25:10.297+00:00

    Hi EstebanPeralta-6089,

    Welcome to our Microsoft Q&A platform!

    You only need to subscribe to event "Click" of the ImageButton and set its "IsVisible" property to false.
    MainPage.xaml

    <ImageButton HorizontalOptions="Center" VerticalOptions="CenterAndExpand"  
        Source="edit.png" Clicked="ImageButton_Clicked" />  
    

    MainPage.xaml.cs

    private void ImageButton_Clicked(object sender, EventArgs e)  
    {  
        (sender as ImageButton).IsVisible = false;  
    }  
    

    ---
    Besides, I prefer to use MVVM to achieve it. You can refer to the following demo.
    MainPage.xaml

    <ContentPage.BindingContext>  
        <loacl:MainPageViewModel/>  
    </ContentPage.BindingContext>  
      
    <StackLayout>  
        <ImageButton HorizontalOptions="Center" VerticalOptions="CenterAndExpand"  
            Source="edit.png" Command="{Binding ClickCommand}" IsVisible="{Binding ShowImage}"/>  
    </StackLayout>  
    

    MainPageViewModel.cs

    class MainPageViewModel : INotifyPropertyChanged  
    {  
        public ICommand ClickCommand { get; set; }  
      
        public MainPageViewModel()  
        {  
            ClickCommand = new Command(Click);  
        }  
      
        bool showImage = true;  
      
        public bool ShowImage  
        {  
            get => showImage;  
            set  
            {  
                showImage = value;  
                OnPropertyChanged("ShowImage");  
            }  
        }  
      
        void Click()  
        {  
            ShowImage = false;  
        }  
      
        public event PropertyChangedEventHandler PropertyChanged;  
      
        public void OnPropertyChanged(string propertyName)  
        {  
            var handle = PropertyChanged;  
            if(handle != null)  
            {  
                handle(this, new PropertyChangedEventArgs(propertyName));  
            }  
        }  
    }  
    

    Regards,
    Kyle


    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.


  2. stone z 76 Reputation points
    2021-09-13T02:51:07.79+00:00
    IsVisible="{Binding IsStartVisible}"
    
    --------------
    
    public bool IsStopVisible{
            get {
                return _isStopVisible;
            }
            set {
                _isStopVisible= value;
                RaisePropertyChanged ("IsStopVisible");
            }
        }
    
    ----
    
    IsStopVisible = true or false;
    
    0 comments No comments