Xamarin上的键盘冻结.窗体搜索栏
本文关键字:窗体 搜索栏 冻结 键盘 Xamarin | 更新日期: 2023-09-27 18:28:14
我有一个自定义实现的控件,它非常占用内存(它基本上为每个组显示一个组标题和一个accordionControl)。CustomControls AccorsionControlGrouping和AccorsionControl都继承自StackLayout,所以我实现了自己的可绑定ItemsSource。当ItemsSource发生更改时,我会主动更改控件的视图。我现在已经优化了这个更改(使用LINQ和BinarySearch等),从5秒开始。每次搜索到0.8秒
这本身不是问题,但每次用户输入一个键时,键盘就会冻结,因此搜索起来非常不方便。
我的搜索栏绑定到这样的ViewModel命令
<SearchBar x:Name="EventSearchBar"
Text="{Binding SearchText}"
SearchCommand="{Binding SearchCommand}"
Placeholder="Suche"/>
搜索命令
private Xamarin.Forms.Command _searchCommand;
public System.Windows.Input.ICommand SearchCommand
{
get
{
_searchCommand = _searchCommand ?? new Xamarin.Forms.Command(UpdateAccordionControlGrouping, CanExecuteSearchCommand);
return _searchCommand;
}
}
private bool CanExecuteSearchCommand()
{
return true;
}
SearchText属性
private string _searchText = string.Empty;
public string SearchText
{
get { return _searchText; }
set
{
if (_searchText != value)
{
_searchText = value ?? string.Empty;
RaisePropertyChanged(nameof(SearchText));
// Perform the search
if (SearchCommand.CanExecute(null))
SearchCommand.Execute(null);
}
}
}
UpdateAccordionControlGrouping
private void UpdateAccordionControlGrouping()
{
// Do some LINQ stuff to group and filter a global collection of data.
// And raise the #OnPropertyChanged event for the ItemsSource of my
// custom control.
}
现在我该如何使整个过程异步?
我已经看了这篇文章,但它对我不起作用。
要异步运行方法"UpdateAccordionControlGrouping()",可以执行以下操作:
private async Task UpdateAccordionControlGrouping()
{
var newgrouping = await Task.Run(() =>
{
//do CPU bound work here
return newgrouping;
});
// assign newgrouping to the correct control
}
请注意,如果您需要更改视图中的任何数据(绑定等),则可能会得到CrossThreadException。您需要使用Dispatcher
,如下所示:UI线程上的Dispatcher.Dispatch
如果使用MvvmLight,则可以使用DispatcherHelper 的DispatchHelper
简单示例