![](/img/trans.png)
[英]How to add tap and long press gesture to image in xamarin.forms?
[英]How to disable the Home tap gesture on home page but not on other pages in Xamarin.Forms?
Xamarin.Forms實現。
我在所有頁面上都有主頁按鈕,並在一個文件中實現它並在所有頁面上呈現它。
現在,我的要求是如果用戶在主頁上並且如果他點擊主頁圖標則不會發生任何事情,即不應該通過輕彈頁面導航到主頁(這是當前的實現)。
我試過,如果我的邏輯,但可能不是它必須如何。 (即)
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>
在每個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,您可以執行您想要的操作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.