How to setup and call an Async method

yams

I'm working on a xamarin android app using Mvvm Light and I've got a method I'm calling and I'd like to change it to an async method but I'm not sure how. Just curious as to how to do this. It's for an Xamarin Android app in Visual Studio's

Call to method :

    protected override void onResume()
    {
         ObservableCollection locObs = Vm.ShowLocations();
    }

Method :

    public ObservableCollection<Location> ShowLocations()
    {
        if (Debugger.IsAttached)
        {
            if (Locations != null)
            {
                var locationsCopy = Locations.ToList();
                Locations = new ObservableCollection<Location>(locationsCopy);
            }
            Election elec = IntentManager.Instance.CurrentElection;
            elec.Locations = new System.Collections.ObjectModel.ObservableCollection<Location>();
            Location loc = new Location();
            loc.Name = "Alan Middle School";
            loc.Address = "300 S. County Farm Road";
            elec.Locations.Add(loc);
            Location loc2 = new Location();
            loc2.Name = "Bill High School";
            loc2.Address = "100 S. County Farm Road";
            elec.Locations.Add(loc2);
            Location loc3 = new Location();
            loc3.Name = "Cameron Fire Station";
            elec.Locations.Add(loc3);
            Location loc4 = new Location();
            loc4.Name = "Danvill Senior Center";
            elec.Locations.Add(loc4);

            Locations = elec.Locations;
        }
        return Locations;
    }
Stephen Cleary

this will be replaced with a service call. It's temporary I'm just asking for tips on Async or examples.

Once you have a service call, you can call it asynchronously with await as such:

var locations = await GetLocationsAsync();
elec.Locations = new ObservableCollection<Location>(locations);

This requires the calling method to be async, as such:

public async Task<ObservableCollection<Location>> ShowLocationsAsync()
{
  var locations = await GetLocationsAsync();
  elec.Locations = new ObservableCollection<Location>(locations);
  return elec.Locations;
}

Which can then be called as:

protected override async void onResume()
{
  ObservableCollection locObs = await Vm.ShowLocationsAsync();
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to call nested Async method

From Dev

How to pass async method delegate in async method call?

From Dev

How to Call an Async Method from within ElapsedEventHandler

From Dev

How to call async method in Autofac registration?

From Dev

How to call an async method, and update the UI from there?

From Dev

How to call async Task<bool> method?

From Dev

How to call an async method on tab focused event?

From Dev

How to call async method inside a method which has return type?

From Dev

Async method call and impersonation

From Dev

call method async with parameters

From Dev

how to call angular factory method from module setup?

From Dev

How to setup callback for mocked object method call by mockito?

From Dev

How to change async method call to prevent forcing async up the call stack

From Dev

How would you call async method in a method which cannot be async in C# without using .Result

From Dev

How can I call async method from constructor?

From Dev

how to call adapter method from other async class

From Dev

How to Call a GWT method whenever an Async RPC is Made

From Java

How to safely call an async method in C# without await

From Java

how do i call async property in Widget build method

From Dev

How to call an anglesharp async method without AsyncEx library?

From Dev

C# - How update a web page after an async method call?

From Dev

How can I call an async method inside an anonymous delegate?

From Dev

How to synchronously call async method from quartz schedule job

From Dev

How to use Reflection to call a method of type "private async Task"

From Dev

How to call async method from sync in C#?

From Dev

Async method call in Razor view

From Dev

Async Task with sync method call

From Dev

C# async method call

From Dev

Properly call anonymous method with async

Related Related

  1. 1

    How to call nested Async method

  2. 2

    How to pass async method delegate in async method call?

  3. 3

    How to Call an Async Method from within ElapsedEventHandler

  4. 4

    How to call async method in Autofac registration?

  5. 5

    How to call an async method, and update the UI from there?

  6. 6

    How to call async Task<bool> method?

  7. 7

    How to call an async method on tab focused event?

  8. 8

    How to call async method inside a method which has return type?

  9. 9

    Async method call and impersonation

  10. 10

    call method async with parameters

  11. 11

    how to call angular factory method from module setup?

  12. 12

    How to setup callback for mocked object method call by mockito?

  13. 13

    How to change async method call to prevent forcing async up the call stack

  14. 14

    How would you call async method in a method which cannot be async in C# without using .Result

  15. 15

    How can I call async method from constructor?

  16. 16

    how to call adapter method from other async class

  17. 17

    How to Call a GWT method whenever an Async RPC is Made

  18. 18

    How to safely call an async method in C# without await

  19. 19

    how do i call async property in Widget build method

  20. 20

    How to call an anglesharp async method without AsyncEx library?

  21. 21

    C# - How update a web page after an async method call?

  22. 22

    How can I call an async method inside an anonymous delegate?

  23. 23

    How to synchronously call async method from quartz schedule job

  24. 24

    How to use Reflection to call a method of type "private async Task"

  25. 25

    How to call async method from sync in C#?

  26. 26

    Async method call in Razor view

  27. 27

    Async Task with sync method call

  28. 28

    C# async method call

  29. 29

    Properly call anonymous method with async

HotTag

Archive