Forum Xamarin.Forms

Dependency Service: set accessibility focus after a button click

Poli97Poli97 Member ✭✭
edited February 2020 in Xamarin.Forms

What I want is to be able to set the accessibility focus to a particular element after the click of a button. I tried to achieve that using dependency service, but it doesn't work, I think due to the fact that I try to convert a Forms.View to an Android.View, but I am not sure if it is the problem, maybe I am doing something else wrong. Do you know how to do that, using dependency service or whatever else?

What I tried, (for the moment only on Android):

MainPage:
`public partial class MainPage : ContentPage
{
Button b1;
Button b2;
IAccessibilityFocusService service;

    public MainPage()
    {
    InitializeComponent();

            service = DependencyService.Get<IAccessibilityFocusService>(DependencyFetchTarget.NewInstance);

    b1 = new Button
            {
                Text = "BUTTON 1",
                BackgroundColor = Color.Blue,
            };

            b2 = new Button
            {
                Text = "BUTTON 2",
            };
    stacklayout.Children.Add(b1);
            stacklayout.Children.Add(b2);
    b1.Clicked += Mybutton_Clicked;
}

private async void Mybutton_Clicked(object sender, EventArgs e)
{
        Console.WriteLine("PIPPO clicked");
        service.ChangeAccessibilityFocus(b2); 
    }

}`

IAccessibilityFocusService:
public interface IAccessibilityFocusService { void ChangeAccessibilityFocus(View v); }

AndroidAccessibilityFocus:
`[assembly: Dependency(typeof(CustomViewAccessibility.Droid.AccessibilityFocus))]
public class AndroidAccessibilityFocus : IAccessibilityFocusService

{

    public void ChangeAccessibilityFocus(View v)
    {
    //first I convert the Forms.Veiw to the native android view, so that I can invoke sendAccessibilityEvent on that view.
            Platform.CreateRendererWithContext(v, Android.App.Application.Context).View.SendAccessibilityEvent(EventTypes.ViewFocused); 

    }
}`

Answers

  • JohnHardmanJohnHardman GBUniversity admin

    @Poli97

    Have you debugged to identify what is null? Is it service in Mybutton_Clicked, v in ChangeAccessibilityFocus, or Platform.CreateRendererWithContext(v, Android.App.Application.Context).View in ChangeAccessibilityFocus ?

    You haven't shown how you have registered the DependencyService. If you haven't done that, service will be null.

  • Poli97Poli97 Member ✭✭

    @JohnHardman said:
    @Poli97

    Have you debugged to identify what is null? Is it service in Mybutton_Clicked, v in ChangeAccessibilityFocus, or Platform.CreateRendererWithContext(v, Android.App.Application.Context).View in ChangeAccessibilityFocus ?

    You haven't shown how you have registered the DependencyService. If you haven't done that, service will be null.

    Ups sorry my fault here forgot to register the DependencyService, anyway service still doesn't work, there is no error now, but it just doesn't do what it is supposed to do, change the focus to "BUTTON 2" after the click of "BUTTON 1". I edited the question. Do you know how to solve?

  • JohnHardmanJohnHardman GBUniversity admin

    @Poli97 said:
    change the focus to "BUTTON 2" after the click of "BUTTON 1". I edited the question. Do you know how to solve?

    I haven't tried it, but I would try putting await Task.Delay(100); before your call to SendAccessibilityEvent. That's just a guess though, so may not work...

  • Poli97Poli97 Member ✭✭

    @JohnHardman said:

    @Poli97 said:
    change the focus to "BUTTON 2" after the click of "BUTTON 1". I edited the question. Do you know how to solve?

    I haven't tried it, but I would try putting await Task.Delay(100); before your call to SendAccessibilityEvent. That's just a guess though, so may not work...

    It means that the return type has to be a Task right?
    Anyway I that
    var renderer = Platform.CreateRendererWithContext(v, Android.App.Application.Context).View; await Task.Delay(100); renderer.SendAccessibilityEvent(EventTypes.ViewFocused);
    but still not working.

  • JohnHardmanJohnHardman GBUniversity admin

    @Poli97 said:
    but still not working.

    I'd have to spend time investigating. Unfortunately, too busy to do that right now. At least the initial null reference exception is now solved. Hopefully somebody else can get you over the final hurdle.

  • Poli97Poli97 Member ✭✭

    @JohnHardman said:

    @Poli97 said:
    but still not working.

    I'd have to spend time investigating. Unfortunately, too busy to do that right now. At least the initial null reference exception is now solved. Hopefully somebody else can get you over the final hurdle.

    Ok thanks anyway!

Sign In or Register to comment.