C# Xamarin表单触发器SearchCommand位于空字符串上的搜索栏中

C# Xamarin表单触发器SearchCommand位于空字符串上的搜索栏中,c#,xamarin.forms,custom-renderer,C#,Xamarin.forms,Custom Renderer,我在XamarinForm上使用了一个搜索栏,问题是每当搜索栏为空时,我按下搜索按钮,搜索命令就不会被触发 我尝试在这个链接上使用自定义渲染器 但它不起作用 public class CustomSearchBarRenderer : SearchBarRenderer { protected override void OnElementChanged(ElementChangedEventArgs<SearchBar> e) { base.OnEl

我在XamarinForm上使用了一个搜索栏,问题是每当搜索栏为空时,我按下搜索按钮,搜索命令就不会被触发

我尝试在这个链接上使用自定义渲染器 但它不起作用

public class CustomSearchBarRenderer : SearchBarRenderer
{
    protected override void OnElementChanged(ElementChangedEventArgs<SearchBar> e)
    {
        base.OnElementChanged(e);
        if (e.OldElement == null)
        {
            this.Control.QueryTextSubmit += (sender, args) =>
            {
                if (string.IsNullOrEmpty(args.Query) && Element.SearchCommand != null)
                    Element.SearchCommand.Execute(null);
            };
        }
    }
}
public类customsearchbarnderer:searchbarnderer
{
受保护的覆盖无效OnElementChanged(ElementChangedEventArgs e)
{
基础。一个要素发生变化(e);
if(e.OldElement==null)
{
this.Control.QueryTextSubmit+=(发送方,参数)=>
{
if(string.IsNullOrEmpty(args.Query)&&Element.SearchCommand!=null)
元素.SearchCommand.Execute(null);
};
}
}
}
请帮助我了解MVVM使用行为。 在XAML中:


我不认为自定义渲染器会有帮助,但您可以使用mySearchBar.TextChanged触发搜索或缓存搜索字符串,这正是我想要避免的。我想实现MVVM。目前,我使用的是使用searchbarl.unfocused在视图modelError XLS0414中触发searchCommand,找不到类型“behaviors:EventToCommandBehavior”。请确认您没有缺少程序集引用,并且所有引用的程序集都已生成。@gattsbr遇到与您相同的问题。已安装并关闭

<SearchBar x:Name="SearchBarVehicles" SearchCommand="{Binding SearchCommand}" Text="{Binding SearchText.Value, Mode=TwoWay}"    SearchCommandParameter="{Binding Text, Source={x:Reference SearchBarVehicles}}" >
    <SearchBar.Behaviors>
        <behaviors:EventToCommandBehavior
            EventName="TextChanged"
            Command="{Binding TextChangeInSearchCommand}"
            />
    </SearchBar.Behaviors>
</SearchBar>
public ICommand TextChangeInSearchCommand => new Command(() => SearchInBlank());
private async void SearchInBlank()
{

    if (string.IsNullOrWhiteSpace(SearchText.Value))
    {
        //Your search in blank.
    }

}