My Xamarin.Forms app has several interfaces (for Dependencys) with implementations for Android and iOS. The methods in them are not async. Now I want to add the implementations for these interfaces for UWP, but several of the needed methods are async so they don't fit the signatures. How do I deal with this? Is the only solution to create a separate interface for UWP?
 
    
    - 26,556
- 38
- 136
- 291
- 
                    could you post the code for your interface? Hard to offer suggestions when there is no code. – Ken Tucker Jun 18 '17 at 17:10
- 
                    Something like `string Method1();` which would need to be in UWP: `TaskMethod1();`. – ispiro Jun 18 '17 at 17:38
- 
                    Can you do async interface for all 3 platforms? – Yuri S Jun 21 '17 at 20:32
- 
                    @YuriS Thanks. That's what deckertron_9000 wrote in his answer. – ispiro Jun 21 '17 at 20:34
2 Answers
In these scenarios I tend to use the Task.FromResult method for the non-async implementations. Example: you have a method on Windows that returns a Task of type bool and you want to implement the same functionality on Android and iOS for methods that return bool.
public interface ISample
{
    public Task<bool> GetABool();
}
On Windows you would just return the Task
public class WindowsSample : ISample
{
    public Task<bool> GetABool()
    {
        // whatever implementation
        return SomeTaskOfTypeBool(); 
    }
}
On Android or iOS you would wrap the value in a Task
public class AndroidSample : ISample
{
    public Task<bool> GetABool()
    {
        // replace with however you get the value.
        var ret = true;
        return Task.FromResult(ret);
    }
}
 
    
    - 3,216
- 20
- 30
- 
                    1This is a nice idea. Instead of trying to sync the async, let's async the sync. If you can't beat them, join them. :) – ispiro Jun 19 '17 at 17:30
- 
                    Every time I see someone calling .Result on a Task, a little piece of me dies. – Will Decker Jun 19 '17 at 17:49
You can not use the await keyword. You have to create a new Task and wait for the Task to finish. A separate interface is not necessary. Exception handling with Tasks can be tricky, inform yourself.
Calling an async method Method1 with return value:
string s = Task.Run(() => Method1()).Result;
Without return value:
Task.Run(() => Method1()).Wait;
.Rest or .Wait block until the Task is completed.
More Details: Calling async methods from non-async code
 
    
    - 34
- 3