question

EstebanPeralta-6089 avatar image
0 Votes"
EstebanPeralta-6089 asked KyleWang-MSFT commented

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

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?

dotnet-csharpdotnet-xamarinforms
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

KyleWang-MSFT avatar image
0 Votes"
KyleWang-MSFT answered KyleWang-MSFT commented

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.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@EstebanPeralta-6089 Dose this answer work for you? If not, you can update any issue here.

0 Votes 0 ·
stonez-0855 avatar image
0 Votes"
stonez-0855 answered
 IsVisible="{Binding IsStartVisible}"
    
 --------------
    
 public bool IsStopVisible{
         get {
             return _isStopVisible;
         }
         set {
             _isStopVisible= value;
             RaisePropertyChanged ("IsStopVisible");
         }
     }
    
 ----
    
 IsStopVisible = true or false;
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.