daniel-luberda / DLToolkit.Forms.Controls

Xamarin.Forms Custom Controls
Apache License 2.0
393 stars 182 forks source link

TagEntryView loses focus when a new tag is added on UWP. #292

Open jaladaGmbH opened 4 years ago

jaladaGmbH commented 4 years ago

On UWP a view loses its focus if it is removed from its parent view. In TagEntryView.ForceReload() you call in line 214 Children.Clear() which removes all child views from your control, including your Entry control.

To prevent your control from losing focus, you should only remove your tag views, not the entry from the layout. A solution could look like this:`

    public void ForceReload()
    {
        if (TagItems == null)
            return;

        List<View> list = Children.ToList();
        foreach (var view in list)
        {
            if (view != TagEntry)
            {
                Children.Remove(view);
            }
        }

        for (int i = 0; i < TagItems.Count; i++)
        {
            View view = null;

            if (TagItemTemplate is DataTemplateSelector templateSelector)
            {
                var template = templateSelector.SelectTemplate(TagItems[i], null);
                view = (View)template.CreateContent();
            }
            else
            {
                view = (View)TagItemTemplate.CreateContent();
            }

            view.BindingContext = TagItems[i];

            view.GestureRecognizers.Add(new TapGestureRecognizer()
            {
                Command = new Command(() => PerformTagTap(view.BindingContext))
            });

            Children.Insert(Children.Count - 1, view);
        }
    }

` Best regards Andreas | jalada GmbH