2017-08-15 47 views
0

我是xamarin mvvm模式的初學者。目前我正在嘗試創建一個搜索欄,從名稱列表中搜索單詞。我試圖在我的視圖模型上的Comman函數上編寫一些代碼,並將其綁定到搜索欄的SearchCommand上。但它沒有奏效。這裏是我的代碼使用mvvm模式的列表視圖上的搜索欄的搜索命令

namespace HelloWorld.ViewModel 
{ 

    public class CustViewModel : INotifyPropertyChanged 
    { 

     private custmodel _custmodel; 


     public custmodel custmodel 
     { 
      get { return _custmodel; } 
      set 
      { 
       _custmodel = value; 
       NotifyPropertyChanged(); 
      } 
     } 

     private string _message; 
     public string message 
     { 
      get { return _message; } 
      set 
      { 
       _message = value; 
       NotifyPropertyChanged(); 
      } 
     } 

     private ObservableCollection<string> _list; 
     public ObservableCollection<string> Items 
     { 
      get 
      { 
     return _list; 
      } 
      set 
      { 
       _list = value; 
       NotifyPropertyChanged(); 
      } 
     } 

     public Command SaveCommand 
     { 
      get 
      { 
       return new Command(() => 
       { 
        message = "Your task : " + custmodel.name + ", " + custmodel.surname + " was successfully saved!"; 
       }); 
      } 
     } 

     private string _bar; 

     public string Bar 
     { 
      get { return _bar; } 
      set { _bar = value; 
     } 
    } 

    public Command SearchCommand 
{ 
    get 
    { 
      return new Command(() => 
      { 
       string keyword = _bar; 
       IEnumerable<String> searchresult = _list.Where(name => name.Contains(keyword)); 
       _list = new ObservableCollection<string>(searchresult); 
       NotifyPropertyChanged(); 
      } 
       ); 


    } 
} 

public CustViewModel() 
{ 

    custmodel = new custmodel 
    { 
     name = "Aasish", 
     surname = "Gurung", 
     email = "[email protected]" 
    }; 
    _list = new ObservableCollection<string>(); 
    _list.Add("Saurab"); 
    _list.Add("Basanta"); 
    _list.Add("Abhishek"); 
    _list.Add("Surace"); 
    _list.Add("Amir"); 

    } 

    public event PropertyChangedEventHandler PropertyChanged; 


    //public event PropertyChangedEventHandler PropertyChanged; 

    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "") 
    { 
     if (PropertyChanged != null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
     } 

    } 
} 

這裏是我的XAML文件

<?xml version="1.0" encoding="utf-8" ?> 
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
     xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
     x:Class="HelloWorld.Styling" 
     BackgroundColor="AntiqueWhite" Title="Hello" 
     xmlns:converters="clr-namespace:HelloWorld.Converters; assembly=HelloWorld"> 

<StackLayout> 
    <SearchBar x:Name="MainSearchBar" Text="{Binding Bar}" 
       SearchCommand="{Binding SearchCommand}"/> 
    <ListView ItemsSource="{Binding Items}"/> 
</StackLayout> 

回答

1

首先,確保你設置你的ContentPageBindingContextCustViewModel

另外,您應該停止向_list分配和添加東西,而是向您的公共Items屬性分配並添加內容。 Items是在分配給它時會觸發NotifyPropertyChanged()方法的方法。

因此改變你的SearchCommand這樣:

return new Command(() => { 
    string keyword = _bar; 
    IEnumerable<String> searchresult = _list.Where(name => name.Contains(keyword)); 

    Items = new ObservableCollection<string>(searchresult); 

    //NotifyPropertyChanged(); //There is no reason to trigger NotifyPropertyChanged on this command each time the getter is run, I would image that this could cause an infinite loop 
}); 
+0

謝謝你,感謝你的幫助。如果搜索欄上的文本爲空,我希望返回帶有完整列表的列表視圖。我該怎麼做? –

+0

@AasishGrg最好問一個新問題,而不是在同一篇文章中詢問多個問題,但是您必須保留一個包含所有項目的單獨列表,或者對數據API進行新的調用(如果有的話) ,並要求提供完整的清單。 – hvaughan3