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?
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?
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.
@EstebanPeralta-6089 Dose this answer work for you? If not, you can update any issue here.
IsVisible="{Binding IsStartVisible}"
--------------
public bool IsStopVisible{
get {
return _isStopVisible;
}
set {
_isStopVisible= value;
RaisePropertyChanged ("IsStopVisible");
}
}
----
IsStopVisible = true or false;
10 people are following this question.