首页 文章

Xamarin表单中的图像点击事件

提问于
浏览
0

Categories MainPage

分类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>

请帮忙 . 我需要的是能够将名为“CategoryName”的变量传递给我的SearchAPI控制器并检索具有该类别的所有数据库条目 . 我的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);
    }

2 回答

  • 0

    Xamarin表单中的图像点击事件

    您需要在xaml中为 Image 控件命名

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

    比,在相同文件的代码后面,创建 TapGestureRecognizer 并添加到 Image

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

    我建议你重新构建你的XAML代码设计 . 查看您的代码段,发布的屏幕截图,以及从您的评论到_281440的答案,您可以通过创建custom view并将信息绑定到它来简化您的工作 . 例如,以下代码:

    <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>
    

    可以变成自定义控件,如:

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

    这将通过 a lot 简化您的XAML . 然后,您可以实现ActionEventHandler属性来处理特定视图的点击时间,然后可以将您的自定义控件转换为:

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

    因为看起来(从你的图像)你将重复使用相同的视图,你应该看看实现FlexLayout而不是 Grid 并使用 Binding s将图像文件,文本和点击处理程序附加到视图,将进一步简化您的XAML和代码结构!

相关问题