2017-10-18 5 views
0

J'ai un problème en essayant de faire fonctionner XLabs autocompleteview, d'abord essayer la commande de recherche ne serait pas au viewmodel, donc je l'ai changé en code derrière, maintenant il va à viewmodel et remplit le modèle mais les éléments n'apparaissent pas sur la vue. La vueXLabs autocompleteview n'affiche pas les résultats après la mise à jour du modèle

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
      xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
      xmlns:controls="clr-namespace:XLabs.Forms.Controls;assembly=XLabs.Forms" 
      x:Class="Tracking.Views.ScanPage" 
      Title="{Binding PageTitle}"> 
    <ContentPage.Padding> 
     <OnPlatform x:TypeArguments="Thickness" 
       iOS="10, 20, 10, 10" 
       Android="20, 20, 20, 20" /> 
    </ContentPage.Padding> 
    <ContentPage.Resources> 
     <ResourceDictionary> 
      <DataTemplate x:Key="SugestionItemTemplate"> 
       <ViewCell Height="60"> 
        <ViewCell.View> 
         <StackLayout HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand"> 
          <Label HorizontalOptions="Start" 
              Text="{Binding Items.text}" 
              VerticalOptions="Center" /> 
         </StackLayout> 
        </ViewCell.View> 
       </ViewCell> 
      </DataTemplate> 
     </ResourceDictionary> 
    </ContentPage.Resources> 
    <ContentPage.Content> 
     <StackLayout VerticalOptions="FillAndExpand" 
       HorizontalOptions="FillAndExpand" 
       Orientation="Vertical" 
       Spacing="1"> 
      <Label HorizontalOptions="FillAndExpand" x:Name="Username" Text="{Binding user}" /> 
      <Label Text="Enter document # or scan barcode:" /> 
      <!-- Also had this but would not bind to viewmodel 
      <controls:AutoCompleteView Placeholder="Enter document #..." 
             HorizontalOptions="FillAndExpand" 
             ShowSearchButton="False" 
             SearchCommand="{Binding searchPrOnum}" 
             SelectedCommand="{Binding selectItem}" 
             Text="{Binding Item, Mode=TwoWay}" 
             SuggestionBackgroundColor="White" 
             SuggestionItemDataTemplate="{StaticResource SugestionItemTemplate}" 
             Suggestions="{Binding Items, Mode=TwoWay}" /> --> 
      <controls:AutoCompleteView Placeholder="Enter document #..." 
             HorizontalOptions="FillAndExpand" 
             ShowSearchButton="False" 
             TextChanged="documentSearch_TextChanged" 
             Text="{Binding Item, Mode=TwoWay}" 
             SuggestionBackgroundColor="White" 
             SuggestionItemDataTemplate="{StaticResource SugestionItemTemplate}" 
             Suggestions="{Binding Items, Mode=TwoWay}" 
             x:Name="documentSearch"/> 

      <Button x:Name="scanCode" Text="Scan Barcode" TextColor="White" BackgroundColor="#362C66" Command="{Binding Scanner}" /> 
     </StackLayout> 
    </ContentPage.Content> 
</ContentPage> 

Le code derrière

public ScanPage() 
{ 
    InitializeComponent(); 

    BindingContext = new ScanPageModel(); 

} 

private void documentSearch_TextChanged(object sender, TextChangedEventArgs e) 
{ 
    var vm = new ScanPageModel(); 
    var val = documentSearch.Text; 
    if (val.Length > 2) 
     vm?.searchPrOnum(val); 

} 

La fonction dans le viewmodel

public void searchPrOnum(string srch) 
{ 
    if (srch.Length > 2) 
    { 
     if (isNumeric(srch)) 
     { 
      Items.Clear();  

      string uri = App.apiurl + $"Productionorder/documentsearch/" + srch + "/";  

      using (var client = new HttpClient()) 
      { 
       var response = client.GetAsync(uri); 

       var items = JsonConvert.DeserializeObject<List<DocumentSearch>>(response.Result.Content.ReadAsStringAsync().Result); 

       var list = (from i in items select new ComboBoxData { text = i.text, value = i.number }).Take(100).ToList(); 

       Items = new ObservableCollection<ComboBoxData>(list); 
      } 
     } 
    } 
    OnPropertyChanged("Items"); 
} 

Répondre

0

Je trouve la question, il est avec le XLabs autocompleteview, il est le fait que j'appelais le viewmodel du code derrière, une fois que j'ai changé l'appel pour lier de la vue, j'ai pu mettre à jour le modèle. Le problème était que PropertyChangedEventHandler était toujours null et que le modèle ne serait pas mis à jour, j'ai besoin d'ajouter un comportement à l'événement Entry on text changed pour déclencher la commande pour rechercher PrO.