0

I am not sure of the terminology for what I'm looking to do, so sorry in advance!

I've found a FilePicker plugin for Xamarin.Forms (https://github.com/Studyxnet/FilePicker-Plugin-for-Xamarin-and-Windows) that implements device-specific functionality for selecting files via the CrossFilePicker class. The way to use leverage this functionality would be something like CrossFilePicker.Current.OpenFile("Filename.txt");

The most important part of this for me is that CrossFilePicker.Current is static and can be accessible from anywhere in the shared layer of my Xamarin.Forms app.

I need to implement a class with the same characteristics. I want to leverage device Accessibility functionality (i.e. determining if a screen reader is enabled) and I need to be able to do so with a static class. My eventual plan is to then wrap this static class so that I can use it for unit tests too.

I don't want to import device libraries into my shared project.

TLDR: I need a static class that implements device-specific functionality.

Any help would be greatly appreciated! Thank you :)

EDIT:

Here are the files I have currently implemented in my project

IAccessibilityService Located in the shared .NET project

namespace Bitspace.Services
{
    public interface IAccessibilityService
    {
        public bool IsScreenReaderEnabled();

        public void Announcement(string message);

        public void NavigationAnnouncement(string message);
    }
}

DeviceAccessibility.cs Located in the shared .NET project

using System;

namespace Bitspace.Services
{
    public class DeviceAccessibility
    {
        private static Lazy<IAccessibilityService> Implementation = new Lazy<IAccessibilityService>(() => CreateAccessibilityService(), System.Threading.LazyThreadSafetyMode.PublicationOnly);

        public static IAccessibilityService Current
        {
            get
            {
                var curr = Implementation.Value;
                if (curr == null)
                {
                    throw new Exception();
                }

                return curr;
            }
        }

        private static IAccessibilityService CreateAccessibilityService()
        {
            return new DeviceAccessibilityImplementation();
        }
    }
}

DeviceAccessibilityImplementation.cs Located in the Android project

using Android.Runtime;

namespace Bitspace.Services
{

    [Preserve (AllMembers = true)]
    public class DeviceAccessibilityImplementation : IAccessibilityService
    {
        public bool IsScreenReaderEnabled()
        {
            return true;
        }

        public void Announcement(string message)
        {
        }

        public void NavigationAnnouncement(string message)
        {
        }
    }
}

If I try to build the project, I get an error on the return new DeviceAccessibilityImplementation(); line in DeviceAccessibility.cs that says DeviceAccessibility.cs(25, 24): [CS0246] The type or namespace name 'DeviceAccessibilityImplementation' could not be found (are you missing a using directive or an assembly reference?)

However, CTRL Clicking on that line takes me to the DeviceAccessibilityImplementation.cs

  • have you looked at the implementation of `FilePicker` and followed the pattern it uses? The ability to access a single instance of a class from anywhere in your app is called a [Singleton](https://stackoverflow.com/questions/2155688/what-is-a-singleton-in-c) – Jason Sep 16 '22 at 11:57
  • I have tried to copy ```FilePicker``` and closely as I can. I understand that the ```CrossFilePicker.cs``` file is where the magic happens, but my app has problems with the ```CreateFilePicker()``` function. If I don't add the conditional directives, I can't build because no definitions for ```FilePickerImplementation()``` can be found even though if I CTRL Click on ```FilePickerImplementation()``` I get intellisense that shows me the correct files. When I do add the conditional directives, then the ```ret``` variable in ```Current``` is always null. – BruceTheBrick Sep 17 '22 at 00:55
  • you need to actually post your code if you expect us to help – Jason Sep 17 '22 at 01:03
  • Sorry, you're completely right @Jason. I have updated my question to include my code. – BruceTheBrick Sep 18 '22 at 09:34
  • your Android project contains a reference to your Shared project - but your Shared project does NOT have a reference to your Android project. If it did this would create a circular reference. That's why XF includes `DepedencyService` - it allows you to inject platform code into your shared project at runtime to avoid this. – Jason Sep 18 '22 at 12:30
  • With that in mind, I feel like I'm left with two options: 1. Using the ```DependencyService``` to resolve an instance of ```AccessibilityService``` or 2. Initialise my singleton manually in each platform by passing that platforms implementation of ```AccessibilityService``` to ```DeviceAccessibility```. Would that be right? – BruceTheBrick Sep 18 '22 at 20:35
  • 1
    I would use `DependencyService`. It has a `RegisterSingleton` method – Jason Sep 18 '22 at 21:03
  • No worries, I'll head down that path. Thanks for the help @Jason! – BruceTheBrick Sep 18 '22 at 22:18

0 Answers0