Help
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Copilot Lvl 2
Message 1 of 2

How can I trigger an event at UserControl when I click a button on Page.

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,

 

private void MyMapViewOnGeoViewTapped(object sender, GeoViewInputEventArgs geoViewInputEventArgs)
        {
            if (MyMapView != null)
            {
                Point screenPoint = geoViewInputEventArgs.Position;

                MapPoint mapPoint = MyMapView.ScreenToLocation(screenPoint);

                if (mapPoint != null)
                {
                    //// point = GetDesiredElevationLocation();
                    //double result = await Dted1Surface.GetElevationAsync(mapPoint);

                    if (MyMapView.IsWrapAroundEnabled)
                    {

                        mapPoint = GeometryEngine.Project(mapPoint, new SpatialReference(3168)) as MapPoint;


                    }

                }

                else
                    return;

                // Get the tapped point, projected to WGS84.
                MapPoint destination = (MapPoint)GeometryEngine.Project(geoViewInputEventArgs.Location,new SpatialReference(3168));

                // Update the destination graphic.
                _endLocationGraphic.Geometry = destination;
                           
                double result1;
                double result2;

                GeodeticDistanceResult distance = GeometryEngine.DistanceGeodetic(start, destination, LinearUnits.Meters, AngularUnits.Degrees, GeodeticCurveType.NormalSection);
                List<MapPoint> mapPoints = new List<MapPoint> { start , destination };
                IReadOnlyList<MapPoint> coordinateList = GeometryEngine.MoveGeodetic(mapPoints, distance.Distance, LinearUnits.Meters, distance.Azimuth1, AngularUnits.Degrees, GeodeticCurveType.NormalSection);

                graphic1.Geometry = coordinateList[1];

                if (distance.Azimuth1 >= 0)
                {
                    result1 = distance.Azimuth1;             
                }
                else
                {
                    result1 = distance.Azimuth1 + 360;                  
                }
                if (distance.Azimuth2 >= 0)
                {
                    result2 = distance.Azimuth2;
                }
                else
                {
                    result2 = distance.Azimuth2 + 360;
                }
                textBox.Text = string.Format($"Distance:{distance.Distance} | Azimuth1:{result1} | Azimuth2:{result2} |Initial Point :410175.775805, 552181.881083 | RSO : {Math.Round(mapPoint.Y,6)},{Math.Round(mapPoint.X,6)}  ");


                //textBox.Text = string.Format("{0} kilometers", (int)distance);
                // Get the points that define the route polyline.
                Esri.ArcGISRuntime.Geometry.PointCollection polylinePoints = new Esri.ArcGISRuntime.Geometry.PointCollection(new SpatialReference(3168))
                {

                (MapPoint)_startLocationGraphic.Geometry,
                destination,
                coordinateList[1]

                };

                // Create the polyline for the two points.
                Polyline routeLine = new Polyline(polylinePoints);


                // Densify the polyline to show the geodesic curve.
                Esri.ArcGISRuntime.Geometry.Geometry pathGeometry = GeometryEngine.DensifyGeodetic(routeLine, 1, LinearUnits.Kilometers, GeodeticCurveType.Geodesic);
                // Apply the curved line to the path graphic.
                _pathGraphic.Geometry = pathGeometry;

            }
              

        }
1 Reply
Commander Lvl 1
Message 2 of 2

Re: How can I trigger an event at UserControl when I click a button on Page.

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.

private MyUserControl _programCode; // the UserControl with program logic
public Page()  // constructor
{
    _programCode = new MyUserControl();  // must have an instance to refer to
}
...
public override void ViewLoaded() // some magic place at which point the button has been created
{
    // register the event handler to the button
    tapButton.GeoViewTapped += _programCode.MyMapViewOnGeoViewTapped;
}

I hope this helps.  You might look at this example to see if it helps your understanding (see "ViewWillAppear").

Please follow-up to let us know how you made out. For good karma, mark a reply as the answer if it helped!