简体   繁体   English

Xamarin表单中的图像点击事件

[英]Image click event in Xamarin forms

Categories MainPage 分类主页

Categories XAML Snippet --> 类别XAML代码段->

                        <StackLayout Grid.Column ="1" Grid.Row ="0"  Orientation ="Vertical" BackgroundColor ="White" Padding ="10" HorizontalOptions ="FillAndExpand">
                            <Image Source ="banking.png" HorizontalOptions ="CenterAndExpand "/>
                            <Label Text ="Finance" FontSize ="Small " HorizontalOptions ="FillAndExpand" HorizontalTextAlignment ="Center"/>
                        </StackLayout>

                        <StackLayout Grid.Column ="2" Grid.Row ="0"  Orientation ="Vertical" BackgroundColor ="White" Padding ="10" HorizontalOptions ="FillAndExpand">
                            <Image Source ="legal.png" HorizontalOptions ="CenterAndExpand "/>
                            <Label Text ="Legal" FontSize ="Small " HorizontalOptions ="FillAndExpand" HorizontalTextAlignment ="Center"/>
                        </StackLayout>

Please help. 请帮忙。 What i need is to be able to pass a variable called "CategoryName" to my SearchAPI Controller and retrieve all database entries that have that category. 我需要的是能够将一个名为“ CategoryName”的变量传递给我的SearchAPI Controller并检索具有该类别的所有数据库条目。 My SearchAPIController is like below 我的SearchAPIController如下所示

  [Route("api/Oppotunities/Search/{keyword}")]
    [ResponseType(typeof(List<Oppotunity>))]
    public async Task<IHttpActionResult> GetOppotunitiesByKeyword(string keyword)
    {
        List<Oppotunity> oppotunities = db.Oppotunities
            .Where(oppotunity => oppotunity.Title.Contains(keyword)
                                 || oppotunity.Description.Contains(keyword)
                                 || oppotunity.Category.Contains(keyword)
                                 || oppotunity.Organisation.Contains(keyword)).ToList();
        if (oppotunities == null)
        {
            return NotFound();
        }

        return Ok(oppotunities);
    } 

Image click event in Xamarin forms Xamarin表单中的图像点击事件

You need to give name to your Image control in xaml 您需要在xaml中为Image控件命名

<Image Source ="banking.png" x:Name="imageFinance" HorizontalOptions ="CenterAndExpand "/>

Than, in code behind of the same file, create TapGestureRecognizer & add to that Image 然后,在同一文件后面的代码中,创建TapGestureRecognizer并将其添加到该Image

var tapFinance = new TapGestureRecognizer();
tapFinance.Tapped += async (s, e) =>
{
 //your code
};
imageFinance.GestureRecognizers.Add(tapFinance);

I would recommend you restructure your XAML code design. 我建议您重组XAML代码设计。 Looking at your snippet, your posted screenshot, and from your comment to CGPA6.4 's answer, you could simplify your work by creating a custom view and bind the information to it. 查看您的代码段,发布的屏幕截图,以及从对CGPA6.4的答案的评论,您可以通过创建自定义视图并将信息绑定到该视图来简化您的工作。 For example, this code below: 例如,下面的代码:

<StackLayout 
    Grid.Column="2" 
    Grid.Row="0"  
    Orientation="Vertical" 
    BackgroundColor="White" 
    Padding="10" 
    HorizontalOptions="FillAndExpand">
    <Image 
        Source="legal.png" 
        HorizontalOptions="CenterAndExpand "/>
    <Label 
        Text="Legal" 
        FontSize="Small" 
        HorizontalOptions="FillAndExpand" 
        HorizontalTextAlignment="Center"/>
</StackLayout>

can be turned into a custom control like: 可以变成一个自定义控件,例如:

<MyCustomControl 
    Grid.Column="2" 
    Grid.Row="0" 
    ImageFile="legal.png"
    Text="Legal" />

This will simplify your XAML by a lot . 这将通过大量的简化您的XAML。 You could then implement an Action or EventHandler property to handle when a specific view is tapped, which could then turn your custom control to: 然后,您可以实现ActionEventHandler属性以在点击特定视图时进行处理,然后可以将自定义控件转换为:

<MyCustomControl 
    Grid.Column="2" 
    Grid.Row="0" 
    ImageFile="legal.png"
    Text="Legal" 
    OnControlTap="OnLegalViewTap"/>

Because it appears that (from your image) you will be using the same view repeatedly, you should then look at implementing a FlexLayout instead of a Grid and use Binding s to attach the image file, text, and tap handler to the view, which will simplify your XAML and code structure even more! 因为看起来(从图像中)您将反复使用同一视图,所以您应该查看实现FlexLayout而不是Grid并使用Binding将图像文件,文本和点击处理程序附加到视图,将进一步简化您的XAML和代码结构!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM