You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MVVM绑定Entry时搜索旧值残留问题求助(避免新增输入控件)

Solution to Search Issue with Entry Controls

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:

  1. Create a Search Criteria Class
    Make a class mirroring the properties of Member used for search, implementing INotifyPropertyChanged:

    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));
        }
    }
    
  2. Update Your ViewModel

    • Add a SearchCriteria property to your ViewModel:
      public SearchCriteria SearchCriteria { get; set; } = new SearchCriteria();
      
    • When CurrentMember updates (on collection item selection), copy its values to SearchCriteria:
      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 SearchCommand to use SearchCriteria instead of CurrentMember:
      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
      });
      
  3. Update XAML Bindings
    Change your Entry controls to bind to SearchCriteria instead of CurrentMember:

    <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

  1. 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, replace EntryName.Text() with EntryName.Text—properties don’t use parentheses.

  2. An object reference is required for the non-static field, method, or property 'member'
    You’re accessing an instance member (like a MemberRepo property) as static. Ensure you’re using an initialized instance of MemberRepo. If App.MemberRepo is meant to be static, confirm it’s properly initialized in your App class.

  3. A method that returns a value must have a return statement in all code paths
    Check the method causing this error (likely SearchMember). 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 02:35:06