locked
How to trigger something in the PCL Project from Android or IOS project? RRS feed

  • Question

  • User331959 posted

    I know that it is possible to trigger some functionality from the PCL in Android project or IOS using the DependencyService. But is it possible to do it the opposite way? I tried using an interface which created in the android project but obviously, it won't work. any solution for this?

    Monday, October 30, 2017 8:43 AM

Answers

  • User171749 posted

    Make an interface in your PCL: public interface ISettingsManager { void WriteLocalFile(string FileName, string Data); string LoadLocalFile(string FileName); }

    Create a class in your platform specific project: public class SettingsManager : ISettingsManager { public string PersonalFolderPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);

            // 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;
            }
        }
    

    You can write to a local file in the iOS project and then check to see the contents using the DependencyService in the PCL. So if you wanted to handle what push notification came in for example, you could make a name for it in the iOS project and handle what happens using the DependencyService on the OnStart method in the PCL.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Monday, October 30, 2017 9:24 AM

All replies

  • User171749 posted

    Make an interface in your PCL: public interface ISettingsManager { void WriteLocalFile(string FileName, string Data); string LoadLocalFile(string FileName); }

    Create a class in your platform specific project: public class SettingsManager : ISettingsManager { public string PersonalFolderPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);

            // 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;
            }
        }
    

    You can write to a local file in the iOS project and then check to see the contents using the DependencyService in the PCL. So if you wanted to handle what push notification came in for example, you could make a name for it in the iOS project and handle what happens using the DependencyService on the OnStart method in the PCL.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Monday, October 30, 2017 9:24 AM
  • User137312 posted

    Just check your references. Your iOS and Android projects should include reference to PCL project. Than try to get to PCL project using it's namespace.

    Monday, October 30, 2017 9:59 AM
  • User331959 posted

    @Seandy ah jeez Rick thanks :smile:

    Monday, October 30, 2017 2:19 PM