【问题标题】:How to disable the Home tap gesture on home page but not on other pages in Xamarin.Forms?如何在 Xamarin.Forms 的主页上禁用主页点击手势而不是其他页面?
【发布时间】:2019-05-08 04:34:18
【问题描述】:

Xamarin.Forms 实施。

我在所有页面上都有主页按钮,并已在一个文件中实现它并在所有页面上呈现它。

现在,我的要求是,如果用户在主页上并且如果他点击主页图标,则不应发生任何事情,即不应通过轻弹页面导航到主页(这是当前的实现)。

我根据我的逻辑尝试了 if else ,但可能并非如此。 (即)

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

这是我的点击手势命令图片

<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>

【问题讨论】:

  • 设置隐藏主页按钮,
  • @Prasanth 我不想隐藏图标我只想在主页而不是其他页面上禁用它的点击事件。请尽快指导。
  • @LucasZhang-MSFT 我不需要当前页面的类型。我只想禁用主页上的点击手势命令,没有其他地方。
  • 您能否在您的 xaml 中提供有关轻按手势命令的代码?
  • 确定这是我的图像与点击手势命令

标签: c# xamarin xamarin.forms uitapgesturerecognizer


【解决方案1】:

在每个 contentPage.xaml 中

设置 contentPage 的名称并将其作为 CommandParameter 的参数传递

例如在 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>

在 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.
     }

    }

}

注意: object s 是 contentPage ,你可以为所欲为。

【讨论】:

  • Command 构造函数有一个附加参数canExecute,它是一个Func&lt;bool&gt;,可用于阻止命令的执行。
猜你喜欢
  • 2018-02-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-21
  • 1970-01-01
  • 2017-09-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多