0

I have one method who get data from API:

        async Task WaterDataForecast()
        {   
            WaterBindingData waterData = await _restServiceData.GetWaterDataForecast(GenerateRequestUri(Constants.EndPoint), GenerateRequestUriStations(Constants.EndPoint));
            BindingContext = waterData;

            var firstCity = waterData.WaterStation.Stations[0].NameEN;
        }

I want to use waterData.WaterStation.Stations[0].NameEN; or firstCity in the constructor class.

My Constructor class:

public MainPage()
    {
        InitializeComponent();

        _restServiceData = new RestServiceData();

        _ = WaterDataForecast();

        CustomPin pin1 = new CustomPin
        {
            Type = PinType.Place,
            Position = new Position(41.59043006333251, 24.766286971618303),
            Name = "Xamarin", 
            Label = "р. Бяла",
            Address = "гр. Смолян",
            CodeNum = 1,
            AlertLevel = 2
        };

        CustomPin pin2 = new CustomPin
        {
            Type = PinType.Place,
            Position = new Position(41.56817473054596, 24.758451447799708),
            Label = "р. Черна",
            Name = "Xamarin",
            Address = "гр. Смолян",
            CodeNum = 2,
            AlertLevel = 2
        };

        customMap.CustomPins = new List<CustomPin> {
            pin1,
            pin2,
        };
        customMap.Pins.Add(pin1);
        customMap.Pins.Add(pin2);

        customMap.MoveToRegion(MapSpan.FromCenterAndRadius(new Position(41.567797926753485, 25.389703182725665), Distance.FromKilometers(70)));

        DatabaseConnection(customMap.CustomPins);
    }

I want to set firstCity on label field in pin1 ?

Can I get example how to do it ?

I initialize _ = WaterDataForecast(); in the constructor but how can I get waterData.WaterStation.Stations[0].NameEN and set on the label in the pin ?

Blagovest Pizhev
  • 161
  • 3
  • 14
  • Related https://stackoverflow.com/questions/23048285/call-asynchronous-method-in-constructor – Hans Kesting Jul 26 '21 at 13:40
  • there's a LOT here that could use refactoring - but to answer your specific question, `firstCity` is declared as a local variable to is only available in the method that declares it. If you declare it as a class variable you can use it anywhere in the class. Further, your entire result is stored in the BindingContext, you can always query the data from there. – Jason Jul 26 '21 at 14:07
  • @Jason is spot on about `firstCity` being a local variable. Another option is to have the `WaterDataForecast` method _return_ the value you want; could be cleaner than adding a new class member. – Sean Skelly Jul 26 '21 at 17:12

1 Answers1

1

You can call the method WaterDataForecast later than initialization of CustomPin , and pass the CustomPin as parameter to the method , update the label inside the task .

Sample code


public MainPage(){
   CustomPin pin1 = new CustomPin
   {
    Type = PinType.Place,
    Position = new Position(41.59043006333251, 24.766286971618303),
    Name = "Xamarin", 
    Address = "гр. Смолян",
    CodeNum = 1,
    AlertLevel = 2
   };

   WaterDataForecast(pin1);
}



  async void WaterDataForecast(CustomPin pin)
  {   
       WaterBindingData waterData = await _restServiceData.GetWaterDataForecast(GenerateRequestUri(Constants.EndPoint), GenerateRequestUriStations(Constants.EndPoint));


       MainThread.BeginInvokeOnMainThread(() =>
       {
           pin.Label = waterData.WaterStation.Stations[0].NameEN;
       });
   }
ColeX
  • 14,062
  • 5
  • 43
  • 240
  • I receive this error when I put WaterDataForecast(pin1) in the constructor: No overload for method 'WaterDataForecast' takes 1 arguments Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call. – Blagovest Pizhev Jul 27 '21 at 06:49
  • Change `async Task WaterDataForecast(CustomPin pin)` to `async void WaterDataForecast(CustomPin pin)` . – ColeX Jul 27 '21 at 07:19