以下是在MasterPage中單擊圖像時導航到另一個頁面的示例。這是MasterPage
中的Image
的XAML。
<Image
x:Name="profileImage"
HeightRequest="100"
WidthRequest="100"
HorizontalOptions="Center"/>
我們將添加一個TapGestureRecogniser
到Image
,然後調用在MasterPage
定義的事件。
public partial class MenuPage : ContentPage
{
public event EventHandler MenuTapped;
public MenuPage()
{
InitializeComponent();
TapGestureRecognizer imageViewTap = new TapGestureRecognizer();
imageViewTap.Tapped+= ImageViewTap_Tapped;
profileImage.GestureRecognizers.Add (imageViewTap);
}
async void ImageViewTap_Tapped (object sender, EventArgs e)
{
if (MenuTapped != null)
MenuTapped (this,EventArgs.Empty);
}
}
我們可以聽在MasterDetailPage
從MasterPage
調用事件並導航到個人資料頁面。
public class DashboardPage : MasterDetailPage
{
DetailPage detailPage;
MenuPage masterPage;
public DashboardPage()
{
detailPage = new DetailPage();
var detailNavigationPage=new NavigationPage(detailPage);
Detail = detailNavigationPage;
masterPage= new MenuPage(){Title="Menu",Icon="ic_menuIcon.png"};
Master = masterPage;
masterPage.MenuTapped+= MasterPage_MenuTapped;
}
async void MasterPage_MenuTapped (object sender, EventArgs e)
{
Detail=new new NavigationPage(new ProfilePage());// If you want to load profile page as `DetailPage`
await Navigation.PushModalAsync(new ProfilePage)// If you want to load profile page as another page modally.
}