C# 隐藏ListView Xamarin表单的复杂问题

C# 隐藏ListView Xamarin表单的复杂问题,c#,xaml,listview,xamarin.forms,xamarin-forms-4,C#,Xaml,Listview,Xamarin.forms,Xamarin Forms 4,我有一个搜索栏,其中属性文本绑定到ViewModel中的字符串属性 我在搜索栏中也有一些行为,因此每次更改文本时,都会使用NewTextValue作为查询字符串在对象列表中进行搜索 我遇到的问题是,在将非空字符串传递给搜索/筛选命令之前,我会使ListView不可见。我曾尝试在几种情况下隐藏ListView,例如,如果从搜索栏中删除所有文本 当从现在可见的列表视图中选择一个项目时,我使用该项目填充搜索栏的文本属性,之后我不能在代码中隐藏它。所有尝试都失败,并且ListView仍然可见。注意:我明

我有一个搜索栏,其中属性文本绑定到ViewModel中的字符串属性

我在搜索栏中也有一些行为,因此每次更改文本时,都会使用NewTextValue作为查询字符串在对象列表中进行搜索

我遇到的问题是,在将非空字符串传递给搜索/筛选命令之前,我会使ListView不可见。我曾尝试在几种情况下隐藏ListView,例如,如果从搜索栏中删除所有文本


当从现在可见的列表视图中选择一个项目时,我使用该项目填充搜索栏的文本属性,之后我不能在代码中隐藏它。所有尝试都失败,并且ListView仍然可见。注意:我明确地单独创建了一个隐藏按钮,并看到它工作了,所以我想知道我是否不能将隐藏视图与设置searchbar文本属性联系起来

看法 如果不在搜索栏中使用行为,您可以尝试使用TextChanged方法本身

<SearchBar x:Name="MySearchBar" Text="SearchText" TextChanged="SearchBar_TextChanged" />
否则,若要使用命令执行此操作,则需要在MyViewModel中添加ICommand的isntance以调用FilterOccupationsList


在此之后,我无法将其隐藏在代码中-您尝试执行此操作的代码在哪里?我没看到。嗨,请检查一下TextChanged的方法。也许当text为null时,方法不显示null。您可以显示TextChanged的代码,我会检查。@Jason我缩短了代码,因此文章不会太长,但是,在使用SetProperty设置SearchText属性后,我得到的是FilteredOccupationsVisible=false。这不起作用。@JuniorJiang MSFT我正在使用PRISMEVENTTO命令,因此当文本更改时,将调用FilterOccupationsList。对不起,我没做到clear@steve也许您可以尝试在模型中更改INotifyPropertyChanged,但也不确定prism是否可以使用它。看一看这个关于。谢谢,我已经解决了它,玩了一会儿真与假的FilteredOccupationsVisible。顺便说一下,DeletegateCommand是一个PrismMvvm框架类。再次感谢您的输入。@steve很好,很高兴解决了它!当你有时间的时候,你可以分享它作为回答。然后其他人会看到解决方案。
public class MyViewModel : BaseViewModel
{
    public DelegateCommand<string> FilterOccupationsListCommand { get; }
    public MyViewModel()
    {
        FilterOccupationsListCommand = new DelegateCommand<string>(FilterOccupationsList);
    }
    private void FilterOccupationsList(string query)
    {
        if (!string.IsNullOrWhiteSpace(query))
        {
            FilteredOccupationsVisible = true;
            var searchResult = Occupations.Where(x => x.Name.ToLower().Contains(query));
            FilteredOccupations = new ObservableCollection<Occupation>(searchResult);
        }
        else
            FilteredOccupationsVisible = false;
    }

    private Occupation _occupation;
    public Occupation Occupation
    {
        get => _occupation;
        set
        {
            SetProperty(ref _occupation, value);
            SearchText = value.Name;
        }
    }

    private string _name;
    public string Name { get => _name; set => SetProperty(ref _name, value); }
    private string _searchText;
    public string SearchText 
    { 
        get => _searchText; 
        set { 
              SetProperty(ref _searchText, value); 
              FilteredOccupationsVisible = false;
            } 
    }

    private bool _filteredOccupationsVisible;
    public bool FilteredOccupationsVisible { get => _filteredOccupationsVisible; set => SetProperty(ref _filteredOccupationsVisible, value); }

    public ObservableCollection<Occupation> _filteredOccupations = new ObservableCollection<Occupation>();
    public ObservableCollection<Occupation> FilteredOccupations { get => _filteredOccupations; set { SetProperty(ref _filteredOccupations, value); } }
}
<SearchBar x:Name="MySearchBar" Text="SearchText" TextChanged="SearchBar_TextChanged" />
MyViewModel myViewModel = new MyViewModel();    

private void SearchBar_TextChanged(object sender, TextChangedEventArgs e)
{
    Console.WriteLine("new -- " + e.NewTextValue + "-- old -- " + e.OldTextValue);
    Console.WriteLine("MyEntry --" + MySearchBar.Text);
    //Here can invoke FilterOccupationsList of MyViewModel 
    myViewModel.FilterOccupationsList(MySearchBar.Text);
}
public class MyViewModel : BaseViewModel
{
    public ICommand FilterOccupationsListCommand { private set; get; }
    ...

    public MyViewModel()
    {
       FilterOccupationsListCommand = new Command<string>((NewTextValue) =>
                {
                    // Pass value to FilterOccupationsList.
                    Console.WriteLine("SearchBar new text --" + NewTextValue);
                    FilterOccupationsList(NewTextValue);
                });    
    }
    ...
}