我有这个XAML:
<TableSection Title="Card Activity">
<TextCell Text="English" StyleId="disclosure" Tapped="openEnglishScorePage"/>
<TextCell Text="Romaji" StyleId="disclosure" Tapped="openRomajiScorePage"/>
<TextCell Text="Kana" StyleId="disclosure" Tapped="openKanaScorePage"/>
<TextCell Text="Kanji" StyleId="disclosure" Tapped="openKanjiScorePage"/>
</TableSection>
在我的.cs中我有这个:
void openEnglishScorePage(object sender, EventArgs e)
{
Navigation.PushAsync(new EnglishScorePage());
}
void openKanaScorePage(object sender, EventArgs e)
{
Navigation.PushAsync(new KanaScorePage());
}
void openKanjiScorePage(object sender, EventArgs e)
{
Navigation.PushAsync(new KanjiScorePage());
}
void openRomajiScorePage(object sender, EventArgs e)
{
Navigation.PushAsync(new RomajiScorePage());
}
有没有办法可以用一个替换四个.cs方法而在XAML中用一个参数调用这个方法?
答案 0 :(得分:1)
是的,您可以,我将向您展示我使用的一些示例代码: 我的XAML代码
<StackLayout Orientation="Horizontal" HorizontalOptions="Center" x:Name="sizestack">
<local:CustomImageButton WidthRequest="120" Clicked="buttonMenu_Clicked" Margin="0,0,-10,0" Image="eigendommen.png" ClassId="Eigendommen" x:Name="buttonGebouwen" HeightRequest="120"/>
<local:CustomImageButton WidthRequest="120" x:Name="buttonHuurcontract" Clicked="buttonMenu_Clicked" ClassId="Huurcontracten" Image="huurcontracten.png" HeightRequest="120" />
</StackLayout>
<StackLayout Orientation="Horizontal" HorizontalOptions="Center">
<local:CustomImageButton WidthRequest="120" Margin="0,-15,-10,0" Clicked="buttonMenu_Clicked" Image="eigenaarshuurders.png" ClassId="Eigenaars" x:Name="buttonEigenaars" TextColor="White" HeightRequest="120" />
<local:CustomImageButton WidthRequest="120" Margin="0,-15,0,0" Clicked="buttonMenu_Clicked" Image="leveranciers.png" ClassId="Leveranciers" x:Name="buttonLeveranciers" HeightRequest="120" />
</StackLayout>
<StackLayout Orientation="Horizontal" HorizontalOptions="Center">
<local:CustomImageButton WidthRequest="120" Margin="0,-15,-10,0" Clicked="buttonMenu_Clicked" Image="meldingen.png" ClassId="AnnouncementPage" x:Name="buttonMeldingen" HeightRequest="120" />
<local:CustomImageButton WidthRequest="120" Margin="0,-15,0,0" Image="taken.png" x:Name="buttonTaken" ClassId="Taken" Clicked="buttonMenu_Clicked" TextColor="White" HeightRequest="120" />
</StackLayout>
在每个CustomImageButton中,我都有一个名为buttontonMenuClicked的事件。还要注意我填写了classId。我做了这个,所以我可以决定去哪个页面。
我的代码背后:
private void buttonMenu_Clicked(object sender, EventArgs e)
{
string naam = ((Button)(sender)).ClassId;
Page navPage = new Page();
switch (naam)
{
case "Eigendommen":
navPage = new Gebouwen() {Title = "Eigendommen" };
break;
case "Leveranciers":
navPage = new Leveranciers() { Title = "Leveranciers" };
break;
case "Eigenaars":
navPage = new Eigenaars() { Title = "Eigenaars/Huurders" };
break;
case "Taken":
navPage = new TakenPage() { Title = "Taken" };
break;
case "Huurcontracten":
navPage = new HuurcontractenPage() { Title = "Huurcontracten" };
break;
default:
navPage = new AnnouncementPage(id) { Title = "Meldingen" };
break;
}
var app = Application.Current as App;
var mainPage = (MenuPage)app.MainPage;
mainPage.SelectMenuNewItem(navPage.Title);
mainPage.Detail = new NavigationPageBar(navPage);
}
所以我在这里请求classid,以便我可以决定去哪个页面。如果你不确定某事,可以提出问题;)