dotMorten / MauiEx

A set of .NET MAUI controls
Apache License 2.0
225 stars 55 forks source link

problems i need add two components in view but not working #69

Closed eduardovedelago closed 4 years ago

eduardovedelago commented 4 years ago

i have a problem i need add two components in view but not working

if add text in one component the other repeat the value added in de first component. this problem occurred if add text programatic or in keyboard

I need to unlink the events

eduardovedelago commented 4 years ago

`using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Runtime.CompilerServices; using System.Text; using System.Threading.Tasks; using dotMorten.Xamarin.Forms; using Xamarin.Forms;

namespace teste4 { // Learn more about making custom code visible in the Xamarin.Forms previewer // by visiting https://aka.ms/xamarinforms-previewer [DesignTimeVisible(true)] public partial class MainPage : ContentPage, INotifyPropertyChanged {

    public List<string> Users { get; }

    public MainPage()
    {

        InitializeComponent();
        this.BindingContext = this;

        Users = new List<string>();
        Users.Add("Bertuzzi");
        Users.Add("Bruna");
        Users.Add("Polly");
        Users.Add("Rodolfo");
        Users.Add("Lester");
    }

    #region PropertyAutoSuggestBox_TextChanged

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    protected bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
    {
        if (EqualityComparer<T>.Default.Equals(storage, value))
        {
            return false;
        }

        storage = value;
        OnPropertyChanged(propertyName);
        return true;
    }

    #endregion

    bool isBusy;

    public bool IsBusy
    {
        get => isBusy;
        set
        {
            if (SetProperty(ref isBusy, value))
                IsNotBusy = !isBusy;
        }
    }

    bool isNotBusy = true;

    public bool IsNotBusy
    {
        get => isNotBusy;
        set
        {
            if (SetProperty(ref isNotBusy, value))
                IsBusy = !isNotBusy;
        }
    }

    private string _pesquisa;
    public string Pesquisa
    {
        get { return _pesquisa; }
        set => SetProperty(ref _pesquisa, value);
    }

    private string _selecionado;
    public string Selecionado
    {
        get { return _selecionado; }
        set => SetProperty(ref _selecionado, value);
    }

    public IEnumerable<string> ObterSugestoes(string text)
    {
        return Users.Where(x => x.StartsWith(text, StringComparison.InvariantCultureIgnoreCase));
    }

    //Evento de AutoComplete
    private void AutoSuggestBox_TextChanged(AutoSuggestBox sender, AutoSuggestBoxTextChangedEventArgs e)
    {
        AutoSuggestBox box = (AutoSuggestBox)sender;

        if (e.Reason == AutoSuggestionBoxTextChangeReason.UserInput)
        {
            if (string.IsNullOrWhiteSpace(box.Text))
                box.ItemsSource = null;
                box.IsSuggestionListOpen = false;
            else
            {
                var suggestions = ObterSugestoes(box.Text);
                box.ItemsSource = suggestions.ToList();
            }
        }
    }

    //Sugestao Selecionada
    private void AutoSuggestBox_SuggestionChosen(AutoSuggestBox sender, AutoSuggestBoxSuggestionChosenEventArgs e)
    {
        var itemselecionado = e.SelectedItem;
        Selecionado = $"Item Selecionado: {itemselecionado}";
    }

    //Evento para pegar se foi utilizada a opçao do usuario ou do auto complete
    private void AutoSuggestBox_QuerySubmitted(AutoSuggestBox sender, AutoSuggestBoxQuerySubmittedEventArgs e)
    {            
        if (e.ChosenSuggestion == null)
        {
            Pesquisa = $"Pesquisa: {e.QueryText} ";
            Selecionado = "Item Selecionado: Nenhum";
        }
        else
            Pesquisa = $"Sugestao: {e.ChosenSuggestion}";
    }        
}

}`

<?xml version="1.0" encoding="utf-8"?>

dotMorten commented 4 years ago

You'll probably need separate event handlers for each control if you want different behaviors. Or perhaps I'm not understanding right?

Dirwaz commented 4 years ago

Greetings,

This is exactly my problem but i have put the 2 controls in 2 different views.

dotMorten commented 4 years ago

I'm not really understanding the problem. Why can't you have two different event handlers - one for each control?

Dirwaz commented 4 years ago

I'm not really understanding the problem. Why can't you have two different event handlers - one for each control?

i did in my code and the same issue exists.

dotMorten commented 4 years ago

The code above both controls uses the same event handlers AutoSuggestBox_TextChanged, AutoSuggestBox_QuerySubmitted and AutoSuggestBox_SuggestionChosen.

Dirwaz commented 4 years ago

The code above both controls uses the same event handlers AutoSuggestBox_TextChanged, AutoSuggestBox_QuerySubmitted and AutoSuggestBox_SuggestionChosen.

check my code in my issue and you will find the event handlers are different.

dotMorten commented 4 years ago

I don't see the difference. They both point to the same set of handlers:

   <teste4:SuggestBoxCustom            
        PlaceholderText="Qual o usuario deve aparecer?"  
        WidthRequest="200"        
        TextChanged="AutoSuggestBox_TextChanged"
        QuerySubmitted="AutoSuggestBox_QuerySubmitted"
        SuggestionChosen="AutoSuggestBox_SuggestionChosen"/>

     <teste4:SuggestBoxCustom            
        PlaceholderText="Qual o usuario deve aparecer?"  
        WidthRequest="200"        
        TextChanged="AutoSuggestBox_TextChanged"
        QuerySubmitted="AutoSuggestBox_QuerySubmitted"
        SuggestionChosen="AutoSuggestBox_SuggestionChosen"/>
Dirwaz commented 4 years ago

I don't see the difference. They both point to the same set of handlers:

   <teste4:SuggestBoxCustom            
        PlaceholderText="Qual o usuario deve aparecer?"  
        WidthRequest="200"        
        TextChanged="AutoSuggestBox_TextChanged"
        QuerySubmitted="AutoSuggestBox_QuerySubmitted"
        SuggestionChosen="AutoSuggestBox_SuggestionChosen"/>

     <teste4:SuggestBoxCustom            
        PlaceholderText="Qual o usuario deve aparecer?"  
        WidthRequest="200"        
        TextChanged="AutoSuggestBox_TextChanged"
        QuerySubmitted="AutoSuggestBox_QuerySubmitted"
        SuggestionChosen="AutoSuggestBox_SuggestionChosen"/>

check here : https://www.dropbox.com/sh/r014a0za2tsnt5x/AACeU4dmlLK2FqplP0e99qgda?dl=0

eduardovedelago commented 4 years ago

I tried with different events.

I also tried to change the focus, without success, in the example sent: yes the same event is being used but I tested it with different events without success.

I will try to explain better: if you add two components in the same view. When I type in one of them, the other receives the same values.

dotMorten commented 4 years ago

Perhaps I'm not understanding what your issue is. You said you just wanted to unlink the events.

Perhaps a simple app that reproduces the issue plus a screen recording of what you're seeing might help

dotMorten commented 4 years ago

Btw, there's a sample in the sample repo with two controls in the same and that works just fine

dotMorten commented 4 years ago

Both "AutoSuggestBox_QuerySubmitted" and "AutoSuggestBox_SuggestionChosen" change the same two labels so naturally both controls would do the exact same thing.

eduardovedelago commented 4 years ago

this movie with the problema https://youtu.be/8tWnaSpRRas

and project example

https://github.com/eduardovedelago/sampledotmorten

dotMorten commented 4 years ago

Thanks but the video is private so I can't see it

eduardovedelago commented 4 years ago

i fix the link https://www.youtube.com/watch?v=8tWnaSpRRas

dotMorten commented 4 years ago

Thanks! That behavior makes absolutely NO sense. I'll try and debug it a bit tonight. Have you had a chance to debug and figure out where the changes are coming from yet?

eduardovedelago commented 4 years ago

I've tried without success

Apparently there is a link between the events or a single instance.

however, by the logic of how the components are instantiated, it really doesn't make any sense.

eduardovedelago commented 4 years ago

the change_text event is called multiple times

dotMorten commented 4 years ago

Figured it out. Fixed in master

eduardovedelago commented 4 years ago

thanks

dotMorten commented 4 years ago

Fix published: https://www.nuget.org/packages/dotMorten.Xamarin.Forms.AutoSuggestBox/1.1.1