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));
}