Xamarin.Forms 如何在首页禁用主页点击手势,而在其他页面不禁用?
How to disable the Home tap gesture on home page but not on other pages in Xamarin.Forms?
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>
in each 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.
}
}
}
注意:对象s是contentPage,你可以做任何你想做的事情。
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>
in each 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.
}
}
}
注意:对象s是contentPage,你可以做任何你想做的事情。