c# - Trying to get .Net Maui Clicked Event to Send values to the Controller from inside a CollectionView - Stack Overflow

admin2025-04-17  1

 <Frame HasShadow="False" Grid.RowSpan="2" StyleClass="framelbl" BackgroundColor="{Binding MtypeColor}" Grid.ColumnSpan="2" HorizontalOptions="End" Grid.Column="1">
 <Frame.GestureRecognizers>
     <TapGestureRecognizer CommandParameter="{Binding .}" Tapped="OnFrameClick" NumberOfTapsRequired="1" />
 </Frame.GestureRecognizers>
 <Label x:Name="pdd" HorizontalTextAlignment="Center" VerticalTextAlignment="Center" Text="{Binding Mtype}"/>
private void OnFrameClick(object sender, EventArgs e)
{
    var selecteditems = (Frame)sender;
    var classId = selecteditems.GestureRecognizers[0];
    var test = classId;
    var testin = ((TapGestureRecognizer)test).CommandParameter;
    var shipments = (Shipment)testin;

    //var ahipment = (e.Parameter) as Shipment;
}

This actually works but this code is bad to say the least. I'm hoping someone can show me how to write this and get the object Shipment From the (object sender) cleanly.

This is what I need and it works but the code syntax is subpar at best if anyone has any suggestions.

 <Frame HasShadow="False" Grid.RowSpan="2" StyleClass="framelbl" BackgroundColor="{Binding MtypeColor}" Grid.ColumnSpan="2" HorizontalOptions="End" Grid.Column="1">
 <Frame.GestureRecognizers>
     <TapGestureRecognizer CommandParameter="{Binding .}" Tapped="OnFrameClick" NumberOfTapsRequired="1" />
 </Frame.GestureRecognizers>
 <Label x:Name="pdd" HorizontalTextAlignment="Center" VerticalTextAlignment="Center" Text="{Binding Mtype}"/>
private void OnFrameClick(object sender, EventArgs e)
{
    var selecteditems = (Frame)sender;
    var classId = selecteditems.GestureRecognizers[0];
    var test = classId;
    var testin = ((TapGestureRecognizer)test).CommandParameter;
    var shipments = (Shipment)testin;

    //var ahipment = (e.Parameter) as Shipment;
}

This actually works but this code is bad to say the least. I'm hoping someone can show me how to write this and get the object Shipment From the (object sender) cleanly.

This is what I need and it works but the code syntax is subpar at best if anyone has any suggestions.

Share edited Jan 31 at 17:49 Deathstalker asked Jan 31 at 0:34 DeathstalkerDeathstalker 8611 gold badge11 silver badges9 bronze badges 4
  • 2 you're mixing up event handlers and commands. To use the CommandParameter, you need to use the Command of the TapGestureRecognizer, not its event – Jason Commented Jan 31 at 0:40
  • 1 Also, CollectionView has a built in selection mechanism that may make this unnecessary – Jason Commented Jan 31 at 2:17
  • 1 Frame is marked as obsolete in .NET MAUI 9, and will be completely removed in a future release. Use Border instead. – Felix Shen Commented Feb 14 at 8:50
  • 1 You may also refer to EventToCommandBehavior. – Felix Shen Commented Feb 26 at 6:27
Add a comment  | 

1 Answer 1

Reset to default 0

You're using the wrong EventArgs class. You need TappedEventArgs

private void OnFrameClick(object sender, TappedEventArgs e)
{
    if(e.Parameter is Shipment shipment)
    {
        //TODO
    }
}
转载请注明原文地址:http://anycun.com/QandA/1744885073a89000.html