在调用网络服务时停止输入 windows phone 文本框

Typing in a windows phone textbox stops while calling a web service

我有一个文本框,用户可以在其中输入内容。一旦有3个字符,调用一个方法从web服务获取信息,打字不流畅。 我认为问题在于,在未收到信息之前,无法输入任何内容。

我的文本框 XAML:

  <toolkit:PhoneTextBox Background="Black" Width="460" VerticalAlignment="Top" BorderThickness="1" Padding="0" BorderBrush="White"  x:Name="searchtextbox" TextWrapping="Wrap" Hint="Buscar" AcceptsReturn="True" FontSize="21.333" FontFamily="Segoe WP SemiLight" Foreground="White" ActionIcon="/Assets/images/appbar/feature.search.png" InputScope="AddressCity" TextChanged="searchtextbox_TextChanged" SelectionForeground="White" CaretBrush="White" LostFocus="searchtextbox_LostFocus" GotFocus="searchtextbox_GotFocus" Height="71" MaxHeight="71" />

我使用 textchanged 方法来捕获信息。

 private async void searchtextbox_TextChanged(object sender, TextChangedEventArgs e)
        {
            this.ListaResultados.ItemsSource = await Protos.getProtoBusqueda(this.searchtextbox.Text);

        }

这是调用 WS 的方法:

public static async Task<ObservableCollection<Resultados> getProtoBusqueda(string txtCajaBusqueda)
        {

            string txtBusqueda = txtCajaBusqueda;


            string url = String.Concat(urlBasic, urlBusqueda, txtBusqueda, format);
            App.resultados.Coordenadas = null;

            try
            {

                using (HttpClient client = new HttpClient())
                {
                    client.BaseAddress = new Uri(url);

                    HttpResponseMessage response = await client.GetAsync(String.Format(url));

                    if (response.IsSuccessStatusCode)
                    {
                        Stream data = await response.Content.ReadAsStreamAsync();                           
                        UserSearchEntityV2 datos = UserSearchEntityV2.ParseFrom(data);

                       //HIDDEN CODE TO SET INFORMATION//   

                            ListOfResults.Add(new Resultados() { Nombre = datos.ResultList[i].Name, Municipio = datos.ResultList[i].Mun, Provincia = bbdd.obtenerProvincia(datos.ResultList[i].Prov), Id = datos.ResultList[i].Id, Tipo = datos.ResultList[i].Type, TipoImagen = imagenTipo, Transporte1 = listaTransportes[0], Transporte2 = listaTransportes[1], Transporte3 = listaTransportes[2], Transporte4 = listaTransportes[3], Transporte5 = listaTransportes[4], Transporte6 = listaTransportes[5], Transporte7 = listaTransportes[6], Transporte8 = listaTransportes[7], IsStop = isStop });

                        }

                    }
                    else
                    {
                        MessageBox.Show("No se ha podido conectar");

                    }
                }
            }
            catch (Exception ex)
            {

            }

            return ListOfResults;

        }

这两种方法都是异步的,所以,我不明白为什么打字这么慢。 谢谢!

我认为问题是当您从结果中分配 this.ListaResultados.ItemsSource 时,它可能以毫秒计 3 次,这让 UI 变得疯狂。

不要分配操作的任何结果以确保它不缺少,只需调用方法而不将其分配给 ItemsSource。

要正确分配 ItemsSource,如果您使用 INotifyPropertyChanged 创建一个视图模型并将其绑定到 ItemsSource,它会更好地管理它