In XAM110 the following (pseudo?) code is shown:
public interface IDialer { bool MakeCall(string number); } public class Dialer { public static Action<Dialer> Create; public bool MakeCall(string number) ... public Dialer(IDialer impl) { ... } }
The only platform specific code shown is
Dialer.Create = () => new Dialer(new PhoneDialer.Droid());
Now I'm wondering what the rest of the code would be in a real implementation. How is MakeCall
in Dialer
implemented? The platform specific part can be something like
public class PhoneDialeriOS : IDialer { public bool MakeCall(string number) { return UIApplication.SharedApplication.OpenUrl(new NSUrl("tel:" + number)); } }
,but how is PhoneDialer
constructed? A call to a method e.g. Droid()
creates a platform specific instance? How?
Also how does the constructor public Dialer(IDialer impl) { }
does look like, when Create
doesn't return something?
Other samples from Xamarin are using DependencyService
. Here are the implementations: iOS, Droid, WinPhone, UWP
I hope someone has some ideas about how the author thought about this.
Answers
I think
PhoneDialer.Droid()
is only possible with a partial class, but here it is only for demonstration purposes without being real code. It should bePhoneDialerDroid
orPhoneDialeriOS
then. The rest could be something like this:But thats only a guess.
This is wrong in my example
and should be this
But the problematic code is this shown in the video:
public static Action<Dialer> Create;
I takes a parameter but doesn't return something. It is impossible to use that. That is an error. There are also some minor errors like typos, but there is no option to inform Xamarin University of the errors in their lectures.
Nevertheless this could be a real implementation:
Why it doesn't use a static factory? Why there are two options for creating an instance? Nobody knows ...