Maui View add events

Lloyd Sheen 1,391 Reputation points
2022-10-08T16:46:35.527+00:00

I have now read about 50 posts "about" this and nothing I have read works.

Read MS docs and nothing.

What I am trying to do should be simple. I have a ContentView control and I want to add events to it. I can find no information on how to do this.

Can anyone link to a post that shows how to define it / raise it / and consume it.

Thanks

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
2,977 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Josh Pierce 1 Reputation point
    2022-10-09T13:15:49.847+00:00

    I normally use MessagingCenter for that purpose. You can Raise a message using

    MessagingCenter.Send<VIEWNAME, DATATYPE>(this, "MESSAGE", DATATOSEND);

    Then you can subscribe to those messages from whatever class you need using

    MessagingCenter.Subscribe<FROMVIEW, Incoming Data Type>(this,"MESSAGE", async (sender, arg) =>
    {
    ADD LOGIC
    });


  2. John Rutterford 5 Reputation points
    2023-09-19T05:39:15.01+00:00

    You can standard Events in a ContentView.

    Basic sample usage:

    public partial class PickList : ContentView
    {
        public event EventHandler<EventArgs> PickListChanged;
    }
    

    Then in your code:

    private void Gesture_Tap(object sender, TappedEventArgs e)
    {         
    	PickListChanged.Invoke(sender,e);     
    }
    

    Then in your page XAML:

    <mycontrols:PickList PickListChanged="Picker_PickListChanged"></mycontrols:PickList>
    
    
    0 comments No comments