How to set focus to next entry on text change in mvvm?

Shiva799Shiva799 Member ✭✭

Hi All,
I'd like to know if it possible to set focus to next entry on text change and that too without using the Focus() method as i have to make sure test cases follow thru it. Right now i have made the it possible using Focus but my test cases fail when it reaches this line od code
var nextEntry = currentEntry.Parent.FindByName<Entry>(nextentryname); if (nextEntry != null) { nextEntry.Focus(); }
If theres a way please let me know
Thanks

Answers

  • burakkurtarirburakkurtarir Member ✭✭
    edited May 31





    private void Entry_Completed(object sender, EventArgs e)
    {
    var entry = sender as Entry;
    var list = (entry.Parent as StackLayout).Children;
    var index = list.IndexOf(entry);
    var nextIndex = (index + 1) >= list.Count ? 0 : index + 1;
    var next = list.ElementAt(nextIndex);
    next?.Focus();
    }

  • burakkurtarirburakkurtarir Member ✭✭
    edited May 31

    And your stacklayout

    `<StackLayout>
                    <Entry Completed="Entry_Completed"/>
                    <Entry Completed="Entry_Completed"/>
                    <Entry Completed="Entry_Completed"/>
                </StackLayout>`
    
  • Shiva799Shiva799 Member ✭✭

    While creating UT for the above method Parent will be null and hence test will fail. I'm doing this in MVVM pattern

  • JohnHardmanJohnHardman GBUniversity mod

    @Shiva799 said:
    Hi All,
    I'd like to know if it possible to set focus to next entry on text change and that too without using the Focus() method as i have to make sure test cases follow thru it. Right now i have made the it possible using Focus but my test cases fail when it reaches this line od code
    var nextEntry = currentEntry.Parent.FindByName<Entry>(nextentryname); if (nextEntry != null) { nextEntry.Focus(); }
    If theres a way please let me know
    Thanks

    Do you want the focus to move on text change or when the Completed event is fired (typically by hitting Return)?

    Do all of the Entry views have the same Parent?

    What platform(s) do you want this to work on?

    Do you only want the focus to move to another Entry, or could the focus move to another type of view, such as a Button or ListView?

    Do your test cases allow for the move of focus not being instantaneous? It won't take long, but it's not instantaneous.

    Remember that you may need to Unfocus() the current Entry before Focus() on the next one.

Sign In or Register to comment.