调用从 main activity 到片段的异步任务
Call on an Async Task from main activity to a fragment
我有一个异步任务需要在我的 bottomsheet 片段中单击按钮时启动。但是,当我使用
从我的底片片段调用该方法时
private async void _navigationButton_Click(object sender, EventArgs e)
{
await ((MainActivity)Activity).Routing();
}
我收到错误:
CS7036 There is no argument given that corresponds to the required formal parameter 'p' of 'MainActivity.Routing(MapPoint)' Maps.
下面的代码是我在主活动中单击地图上的地图点时获取路线的方法。
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;
}
如果Fragment中没有mappoint,可以先在Mainactivity中定义一个public方法,在该方法中调用Routing(mapPoint)
。
主要Activity
public void MyMethod(){
Routing(mapPoint);
}
片段
private async void _navigationButton_Click(object sender, EventArgs e)
{
await ((MainActivity)Activity).MyMethod();
}
我有一个异步任务需要在我的 bottomsheet 片段中单击按钮时启动。但是,当我使用
从我的底片片段调用该方法时 private async void _navigationButton_Click(object sender, EventArgs e)
{
await ((MainActivity)Activity).Routing();
}
我收到错误:
CS7036 There is no argument given that corresponds to the required formal parameter 'p' of 'MainActivity.Routing(MapPoint)' Maps.
下面的代码是我在主活动中单击地图上的地图点时获取路线的方法。
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;
}
如果Fragment中没有mappoint,可以先在Mainactivity中定义一个public方法,在该方法中调用Routing(mapPoint)
。
主要Activity
public void MyMethod(){
Routing(mapPoint);
}
片段
private async void _navigationButton_Click(object sender, EventArgs e)
{
await ((MainActivity)Activity).MyMethod();
}