简体   繁体   English

如何禁用主页上的Home轻触手势,但不禁用Xamarin.Forms中的其他页面?

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

Xamarin.Forms implementation. Xamarin.Forms实现。

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 在每个contentPage.xaml中

set the name of contentPage and pass it as Parameter of CommandParameter 设置contentPage的名称并将其作为CommandParameter的参数传递

For example in MainPage 例如在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 并在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. 注意:对象s是contentPage,您可以执行您想要的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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