MVVM绑定Entry时搜索旧值残留问题求助(避免新增输入控件)
Core Fix: Separate Search Criteria from Selected Member
The root problem is you’re reusing CurrentMember (the selected collection item) for both display and search input. When clearing an Entry, the binding either doesn’t update CurrentMember as expected or you risk modifying the actual member object in your collection. Here’s how to fix it without adding new controls:
Create a Search Criteria Class
Make a class mirroring the properties ofMemberused for search, implementingINotifyPropertyChanged:public class SearchCriteria : INotifyPropertyChanged { private string _cardNum; public string CardNum { get => _cardNum; set { _cardNum = value; OnPropertyChanged(); } } // Add other searchable properties (name, phone, etc.) with the same pattern public event PropertyChangedEventHandler PropertyChanged; private void OnPropertyChanged([CallerMemberName] string propertyName = null) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } }Update Your ViewModel
- Add a
SearchCriteriaproperty to your ViewModel:public SearchCriteria SearchCriteria { get; set; } = new SearchCriteria(); - When
CurrentMemberupdates (on collection item selection), copy its values toSearchCriteria:private Member _currentMember; public Member CurrentMember { get => _currentMember; set { _currentMember = value; OnPropertyChanged(); // Copy values to search criteria if (_currentMember != null) { SearchCriteria.CardNum = _currentMember.CardNum; // Copy other relevant properties here } } } - Modify
SearchCommandto useSearchCriteriainstead ofCurrentMember:public ICommand SearchCommand => new Command(() => { var results = App.MemberRepo.SearchMember( SearchCriteria.CardNum, SearchCriteria.Name, SearchCriteria.Phone // Pass other search criteria properties ); // Update your collection view with the results });
- Add a
Update XAML Bindings
Change your Entry controls to bind toSearchCriteriainstead ofCurrentMember:<Entry Text="{Binding SearchCriteria.CardNum, Mode=TwoWay}" /> <!-- Update other Entries to use SearchCriteria properties -->
Now, clearing an Entry will set the corresponding SearchCriteria property to an empty value, ensuring your search uses the correct input. CurrentMember remains unchanged, so your selected collection item isn’t modified accidentally.
Fixing Your Code Errors
Non-invocable member 'name' cannot be used like a method
You’re trying to access a property as if it were a method. For example, replaceEntryName.Text()withEntryName.Text—properties don’t use parentheses.An object reference is required for the non-static field, method, or property 'member'
You’re accessing an instance member (like aMemberRepoproperty) as static. Ensure you’re using an initialized instance ofMemberRepo. IfApp.MemberRepois meant to be static, confirm it’s properly initialized in yourAppclass.A method that returns a value must have a return statement in all code paths
Check the method causing this error (likelySearchMember). Ensure every possible code path returns a value matching the method’s return type. For example, return an empty list if no results are found:public List<Member> SearchMember(string cardNum, string name) { var results = // Your filtering logic here return results ?? new List<Member>(); // Guarantee a return value }
内容的提问来源于stack exchange,提问作者Jacky

