Search code examples
c#xamarin.androidarcgis-runtime

Call on an Async Task from main activity to a fragment


I have an async task that needs to initilize on a button click inside my bottomsheet fragment. However when I call on the method from my bottomsheet fragment with

  private async void _navigationButton_Click(object sender, EventArgs e)
   {          
       await ((MainActivity)Activity).Routing();
   }

I get the error:

CS7036 There is no argument given that corresponds to the required formal parameter 'p' of 'MainActivity.Routing(MapPoint)' Maps.

The code below is my method for getting a route when clicking on a mappoint on the map in my mainactivity.

try
{
    _textToSpeech = new TextToSpeech(this, this, "com.google.android.tts");

    Stop stop0 = new Stop(_startPoint) { Name = "Starting Point" };
    // Stop stop1 = new Stop(_endPoint) { Name = "EndPoint" };

    if (_endPoint == null)
    {
        await _geocoder.ReverseGeocodeAsync(p);
        _endPoint = p;
        RouteTask routeTask = await RouteTask.CreateAsync(_routingUri);
        RouteParameters routingParameters = await routeTask.CreateDefaultParametersAsync();
        List<Stop> stops = new List<Stop> { new Stop(_startPoint), new Stop(_endPoint) };
        routingParameters.SetStops(stops);
        RouteResult result = await routeTask.SolveRouteAsync(routingParameters);

        Route firstRoute = result.Routes.First();

        _routeAheadGraphic = new Graphic(firstRoute.RouteGeometry) { Symbol = new SimpleLineSymbol(SimpleLineSymbolStyle.Solid, System.Drawing.Color.Blue, 4) };

        SimpleLineSymbol(SimpleLineSymbolStyle.Dash, System.Drawing.Color.Red, 2) }; _myMapView.GraphicsOverlays[0].Graphics.Add(_routeAheadGraphic);

        await _myMapView.SetViewpointGeometryAsync(firstRoute.RouteGeometry, 100);

        return;
}

Solution

  • If the mappoint is not available in Fragment, you could define a public method in Main activity first , call Routing(mapPoint) inside that method .

    Main Activity

     public void MyMethod(){
         Routing(mapPoint);
     }
    

    Fragment

    private async void _navigationButton_Click(object sender, EventArgs e)
    {          
        await ((MainActivity)Activity).MyMethod();
    }