Acessing shared code from native

I am new to this and one thing I have trouble of finding information about, is how to access shared code from native code. The other way around is ok, and there is multiple resources and examples about DependencyService and how to create native spesific code, and accessing it from shared code.

But the other way around? Some native event triggers something which should modify something in the shared space. I can access methods like App.Current.MainPage.DisplayAlert, but how about custom methods?

Answers

  • seanydaseanyda GBMember ✭✭✭✭✭
    edited October 2017

    I might have misunderstood the question, but if you're using Xamarin.Forms, all your main code is shared across the portable class library, so you will be wanting as much code as possible to be in there and as little as possible in the platform specific projects (because if there is code in the native projects, it means you'll be rewriting it 2-3 times).

    So you'll only want to be taking code from the platform specific projects, you won't want code from the PCL if you're in the platform specific project. (correct me if I'm wrong)

    So this is what you will want to refer to:
    https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/dependency-service/introduction/

    If I wanted to access a reference that is inaccessible from the PCL like using System.IO. I would have to create an interface in the PCL, and a method in the platform specific like the below:

    public void WriteLocalFile(string FileName, string Data)
            {
                string filePath = Path.Combine(PersonalFolderPath, FileName);
                File.WriteAllText(filePath, Data);
            }
    

    This code will work in the platform specific, not the PCL. I would have an interface which contains this line so I can access that from the shared project

    void WriteLocalFile(string FileName, string Data);

    and be able to call it like DependencyService.Get<ISettingsManager>().WriteLocalFile("Test", "Test");

  • nehneh NOMember

    Thank you for your answer, but your answer explains shared->native, which I already find a ton of information about. I want to somehow influence the shared code from native. The other way.

    Lets say there is some event, like a remote notification which is received in the native android project. I send a notification thought Google Cloud Messaging (GCM), which is received in the android native. This could be a notification that some external data has changed, and needs to be updated. Then I want the received native event, to trigger something in the shared project. This could be something like fetching the updated information.

    How is something like this handeled?

  • seanydaseanyda GBMember ✭✭✭✭✭

    @neh said:
    Thank you for your answer, but your answer explains shared->native, which I already find a ton of information about. I want to somehow influence the shared code from native. The other way.

    Lets say there is some event, like a remote notification which is received in the native android project. I send a notification thought Google Cloud Messaging (GCM), which is received in the android native. This could be a notification that some external data has changed, and needs to be updated. Then I want the received native event, to trigger something in the shared project. This could be something like fetching the updated information.

    How is something like this handeled?

    The way I handle push notifications is a little different. For each type of push notification I get I write to a local file in the native project. Then on the StartUp event of the app I check to see what local file has contents and base what the app should do on startup based on the contents of those local files.

    Just to clear things up a little more, say if something is triggered in the native project, you can use the below methods

    // Write Information to a Local File
            public void WriteLocalFile(string FileName, string Data)
            {
                string filePath = Path.Combine(PersonalFolderPath, FileName);
                File.WriteAllText(filePath, Data);
            }
    
            // Load Information from a Local File
            public string LoadLocalFile(string FileName)
            {
                string filePath = Path.Combine(PersonalFolderPath, FileName);
                if (File.Exists(filePath)) return File.ReadAllText(filePath);
                return null;
            }
    

    and load data from the PCL or the native app and it'll contain whatever information you want it to hold. You can store bytes, text or whatever. You'll just need to tweak the methods slightly.

  • joshvivier86joshvivier86 Member
    edited July 2018

    Hi, did you figure this out without writing to a file?

    EDIT: I ended up using the MessagingCenter.

  • wondererwonderer Member ✭✭

    Hi, to get back this calling shared code from native app, I want to create services in ios and android as explained in xam university vid xamarin. Forms backgrounding
    I Vid shows how to call the native code to start the background worker with messaging center,

    I will not need much messaging(the service will sync server to local sqlite)

    I have found d remarks, that after creation of the Droid and ios services, I should be able to call a shared coded sub,

    Does anyone have some idea,sample etc

    Thanks

    Walter

Sign In or Register to comment.