简体   繁体   中英

How to disable the Home tap gesture on home page but not on other pages in Xamarin.Forms?

Xamarin.Forms implementation.

I have home button on all pages and have implemented it in one file and render it on all pages.

Now, my requirement is if the user is on home page and if he taps home icon nothing should happen ie should not navigate to home page by flicking the page(this is the current implementation).

I tried if else to my logic but may be that is not how it has to be. (ie)

if
{
  //user on home page. Do nothing. 
}
else
{
  //navigate to Home.
}

Here is my image with tap gesture command

<Image Grid.Row="0" Grid.Column="0" Source="OneD_Icon_Small.png" HorizontalOptions="StartAndExpand" Margin="5,5,0,0">
    <Image.GestureRecognizers>
        <TapGestureRecognizer Command="{Binding HomeCommand}" NumberOfTapsRequired="1" />
    </Image.GestureRecognizers>
</Image>

in each contentPage.xaml

set the name of contentPage and pass it as Parameter of CommandParameter

For example in MainPage

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:App12"
             x:Name="myPage"
             x:Class="App12.MainPage">
    <StackLayout VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" >

        <Image Source="OneD_Icon_Small.png" HorizontalOptions="StartAndExpand" Margin="5,5,0,0">
            <Image.GestureRecognizers>
                <TapGestureRecognizer Command="{Binding HomeCommand}"  CommandParameter="{Binding .,Source={x:Reference myPage}}" NumberOfTapsRequired="1" />
            </Image.GestureRecognizers>
        </Image>
    </StackLayout>

</ContentPage>

And in the ViewModel

public class TapViewModel : INotifyPropertyChanged
 {        
   ICommand homeCommand;
   public TapViewModel()
   {
      // configure the TapCommand with a method
      homeCommand = new Command(OnTapped);
   }
   public ICommand HomeCommand
   {
     get { return homeCommand; }
   }

   public event PropertyChangedEventHandler PropertyChanged;

   void OnTapped(object s)
   {
     var page = s as ContentPage;

     if (page.GetType() == typeof(MainPage))
     {
        //user on home page. Do nothing.
     }

     else
     {
        //navigate to Home.
     }

    }

}

Note: object s is the contentPage ,and you can do something you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM