2017-06-29 76 views
0

我想使用MVVM創建Xamarin搜索頁面。我在我的MainWindowViewModel中創建了一個邏輯,在用戶將一個字符輸入到searchar中後,必須更新我的ListView。但我有這樣的結果:In some reason UI is not updating創建MVVM搜索頁面Xamarin

我不知道我做錯了。 ,也是我想asynchronysly調用ExecuteSearchCommand,如果您展示如何正確地實現它,我會approsiate。 謝謝。

<ContentPage.BindingContext> 
    <ViewModels:MainWindowViewModel/> 
    </ContentPage.BindingContext> 

<ContentPage.Content> 
    <StackLayout> 

     <SearchBar SearchCommand="{Binding SearchCommand}" 
        Text="{Binding EnteredText}" 
        /> 
     <Label Text="{Binding EnteredText}"></Label> 
     <ListView x:Name="lstContatos" ItemsSource="{Binding MyList}" HasUnevenRows="True"> 
      <ListView.ItemTemplate> 
       <DataTemplate> 
        <ViewCell> 
         <StackLayout> 
          <Grid MinimumHeightRequest="80" HeightRequest="120" HorizontalOptions="FillAndExpand" > 
           <Grid.RowDefinitions> 
            <RowDefinition/> 
            <RowDefinition/> 
           </Grid.RowDefinitions> 
           <Grid.ColumnDefinitions> 
            <ColumnDefinition/> 
            <ColumnDefinition/> 
           </Grid.ColumnDefinitions> 

           <Label Grid.Row="0" Text="{Binding PhrasalVerb}"/> 
           <Button Text="Delete" Grid.Column="1" BackgroundColor="Black" HeightRequest="30" WidthRequest="40" IsVisible="True"/> 

          </Grid> 
         </StackLayout> 
        </ViewCell> 
       </DataTemplate> 
      </ListView.ItemTemplate> 
     </ListView> 
    </StackLayout> 
</ContentPage.Content> 

這是我的ViewModel結合到View

 public string EnteredText 
     { 
     get { return enteredText; } 
     set 
     { 
      enteredText = value; 
      this.SearchCommand.Execute(null); 
      OnPropertyChanged(nameof(EnteredText)); 
     } 
    } 


    void ExecuteSearchCommand(object parameter) 
    { 
     if (enteredText.Length>=1) 
     { 
      MyList = new ObservableCollection<PhV_Get>(phrasalVerbGet 
       .Where(x => x.PhrasalVerb.ToLower() 
       .Contains(enteredText.ToLower())).ToList()); 
     } 
     else 
     { 
      MyList = phrasalVerbGet; 
     } 

    } 

    public ObservableCollection<PhV_Get> MyList 
    { 
     set 
     { 
      phrasalVerbGet = value; 
      OnPropertyChanged(nameof(MyList)); 
     } 
     get 
     { 
      return phrasalVerbGet; 
     } 
    } 

    public Command SearchCommand { 
     get 
     { 
      return new Command(ExecuteSearchCommand, 
      CanExecuteSeachCommand); 
     } 
    } 

    public bool CanExecuteSeachCommand(object parameter) 
    { 
     return true; 
    } 


    public event PropertyChangedEventHandler PropertyChanged; 

    private void OnPropertyChanged(string propertyName) 
    { 
     this.PropertyChanged?.Invoke(this, new 
        PropertyChangedEventArgs(propertyName)); 
    } 

回答

0

MyList = new ObservableCollectionExecuteSearchCommand是一個沒有沒有。你的listview被綁定到舊的ObservableCollection上 - 當你創建一個新的時,你打破了綁定,視圖不會更新,因爲它不知道新的。

當你需要改變一個觀察的集合的內容,像這樣做(在僞代碼):

MyList.Clear(); 
foreach (thing in myListOfThings) 
{ 
    MyList.Items.Add(thing); 
} 

要更新ListView控件綁定到集合的方式,和ListView控件會查看更改。