How can I trigger an event at UserControl when I click a button on Page. #21378
-
Hi, I having a problem that get me quite a long of time to understand. I still new in C# and I try to grasp the method that i can trigger an event on UserControl when I click a button on Page . I put all the navigation button on Page in my project to make me easier to make a UI transition and my program code on UserControl. Below is the code that i need to trigger when a button is click. Can anybody show me any guide or solution so that i can solve my problem? Best regards,
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
What you need to do is register your event handler to the control which will raise the event. Also called “subscribing” to the event. You mention that you have a Button on your Page which you want to raise the event. That button needs to have its GeoViewTapped handler set to your MyMapViewOnGeoViewTapped function. The fact that said function is in your UserControl isn’t important, as long as the Page has an instance of the UserControl. I’m not sufficiently familar with Xamarin.Forms to know whether the method is called “OnLoad”, or “LoadView”, etc. At some point in the Page logic, your Button control has been created: _after_ that point, you register the event handler. Then, when the button is tapped (and presumably causing the GeoViewTapped event to be raised), your event handler will be called.
I hope this helps. You might look at this example to see if it helps your understanding (see “ViewWillAppear”). |
Beta Was this translation helpful? Give feedback.
What you need to do is register your event handler to the control which will raise the event. Also called “subscribing” to the event.
You mention that you have a Button on your Page which you want to raise the event. That button needs to have its GeoViewTapped handler set to your MyMapViewOnGeoViewTapped function. The fact that said function is in your UserControl isn’t important, as long as the Page has an instance of the UserControl.
I’m not sufficiently familar with Xamarin.Forms to know whether the method is called “OnLoad”, or “LoadView”, etc. At some point in the Page logic, your Button control has been created: _after_ that point, you register the event handler. Then, when the bu…