也许此时您已经解决了您的问题,但如果您仍然在苦苦挣扎,我很高兴在这里分享一个示例,其中包含您实现目标所需的基本成分。
首先,正如其他用户已经提到的,您需要知道 Xamarin.Forms 视图支持所谓的GestureRecognizers。
关注docu:
您可以将手势识别器添加到视图中...
向此集合添加项目会将手势事件与
这个元素。这对于本机的元素不是必需的
支持输入,例如按钮。
所以,虽然您可以使用Button 来触发事件是对的,但您应该知道大多数 视图都承认手势识别器,因此您也可以在点击a 时触发事件StackLayout,或Image等
下面是一个简单的例子,它试图模仿你分享的一张图片。
注意:如果您想复制粘贴以下代码,请记住 XAML 中的 Image.Source 设置为“pizza.jpg”,您必须将自己添加到不同平台项目中的图像。在 Android 项目中将图像添加到 Resources.drawable
XAML
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MyApp.Page1"
BackgroundColor="Black">
<Frame BorderColor="White"
BackgroundColor="Transparent"
CornerRadius="10"
Margin="20"
Padding="10"
HorizontalOptions="CenterAndExpand"
VerticalOptions="CenterAndExpand">
<StackLayout BackgroundColor="Transparent"
Spacing="10">
<Image Source="pizza.jpg"
WidthRequest="200"
HeightRequest="200"/>
<Label Text="Bambini"
TextColor="White"
FontAttributes="Bold"
BackgroundColor="Transparent"
FontSize="Medium"
HorizontalOptions="CenterAndExpand"/>
<!--The following stack is not visible by default-->
<StackLayout x:Name="priceStack"
BackgroundColor="Transparent"
Orientation="Horizontal"
IsVisible="False"
Spacing="5">
<StackLayout BackgroundColor="White"
HorizontalOptions="FillAndExpand">
<Label Text="P"
FontSize="Medium"
HorizontalTextAlignment="Center"
FontAttributes="Bold"/>
<Label Text="$8.20"
FontSize="Medium"
HorizontalTextAlignment="Center"/>
<StackLayout.GestureRecognizers>
<TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped_1"/>
</StackLayout.GestureRecognizers>
</StackLayout>
<StackLayout BackgroundColor="White"
HorizontalOptions="FillAndExpand">
<Label Text="M"
FontSize="Medium"
HorizontalTextAlignment="Center"
FontAttributes="Bold"/>
<Label Text="$9.90"
FontSize="Medium"
HorizontalTextAlignment="Center"/>
<StackLayout.GestureRecognizers>
<TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped_1"/>
</StackLayout.GestureRecognizers>
</StackLayout>
<StackLayout BackgroundColor="White"
HorizontalOptions="FillAndExpand">
<Label Text="G"
FontSize="Medium"
HorizontalTextAlignment="Center"
FontAttributes="Bold"/>
<Label Text="$18.20"
FontSize="Medium"
HorizontalTextAlignment="Center"/>
<StackLayout.GestureRecognizers>
<TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped_1"/>
</StackLayout.GestureRecognizers>
</StackLayout>
</StackLayout>
</StackLayout>
<Frame.GestureRecognizers>
<TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped"/>
</Frame.GestureRecognizers>
</Frame>
</ContentPage>
后面的代码
using System;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace MyApp
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class Page1 : ContentPage
{
public Page1 ()
{
InitializeComponent ();
}
private void TapGestureRecognizer_Tapped(object sender, EventArgs e)
{
priceStack.IsVisible = !priceStack.IsVisible;
}
private async void TapGestureRecognizer_Tapped_1(object sender, EventArgs e)
{
await App.Current.MainPage.DisplayAlert("Confirmation", "Should we send the order?", "Yes", "Cancel");
}
}
}
结果
应用程序启动并显示菜单:
如果您点击该项目,价格会出现:
最后,如果您点击价格,您会看到一个确认对话框:
最后,但并非最不重要的
看看令人惊叹的Xamarin.Forms docu,不要忘记阅读令人愉快的book of Ch. Petzold(只是学习 Xamarin.Forms 的大量资源的两个示例!)。
我希望你能从中有所收获:)