Call out to another cs file that can do Android specific code

I'm failing on something that seems trivial, but as I search and search I can't find anything to explain or show me how to do it. I probably just need the right keyword.

So I'm making the hello world app as per here http://developer.xamarin.com/guides/cross-platform/xamarin-forms/introduction-to-xamarin-forms/ for android and I'm up to the bit about detecting a touch on the list item. But in order to make a Dialog box(seems the DisplayAlert fuction is iOS only ) or even Console.WriteLn I can't do that in the main helloworld.cs file. That code can only be written in the Helloworld.Droid's MainActivity.cs or I guess any other cs file I make inside that subproject.

The kicker is how do I call something in Helloworld.Droid from within the main helloworld.cs file?

Posts

  • adamkempadamkemp USInsider, Developer Group Leader mod

    To do platform-specific things you can use a DependencyService.

  • DanielHotopDanielHotop USMember

    Thanks. That does the trick.

    For those who come after me..

    Once again add this [assembly] attribute above the class (and outside any namespaces that have been defined), including > any required using statements.

    [assembly: Xamarin.Forms.Dependency (typeof (TextToSpeech_WinPhone))]

    Don't listen to this, it is garbage and will just give you compile errors
    Inseted remove all namespaces in the file and put it above the class line and after using statements.
    Otherwise you will just get compile errors.
    e.g.

    using System;
    using helloworld;

    //namespace helloworld.Droid
    //{
    [assembly: Xamarin.Forms.Dependency (typeof (PlatformGeneral_Android))]
    public class PlatformGeneral_Android : Java.Lang.Object, IPlatformGeneral
    {

    also don't forget to add a using statement for the Shared class namespace otherwise it won't know what ISomething is.

Sign In or Register to comment.