Search Box
Overview
SearchBoxViewModel and SearchBoxView form a reusable search control for Asv.Avalonia applications. The view displays a text input, a refresh or cancel button, and a progress indicator. The view model runs an application-provided SearchDelegate whenever the text changes.
The component provides:
optional debouncing for text changes;
cancellation of a running search when a new query starts;
determinate and indeterminate progress indication;
explicit refresh and clear operations.
SearchBoxViewModel does not search a collection by itself. The callback supplied by the application decides how the query is applied, how progress is reported, and how cancellation is handled.
Usage
Create the synchronized collection view and pass its filtering callback to SearchBoxViewModel:
Add the view directly to XAML and bind its data context to the view model property:
The initial Text value does not run the callback. Call Refresh() after initialization when the initial contents of the target view must be loaded through the same callback:
Search Behavior
When throttleTime is set, the component waits for that interval of inactivity before invoking the callback. Without it, text changes start the search immediately.
The callback receives three values:
textis the current query.progressupdates the progress bar. Report values from0to1for determinate progress. Until a value is reported, the control usesdouble.NaNand shows an indeterminate progress bar.cancelis canceled when the user presses the cancel button, when a new query replaces a running query, or when the caller's cancellation token is canceled.
Query(...) and Refresh(...) bypass the debounce interval and start the callback immediately.
API
SearchDelegate
Represents the asynchronous callback that applies a search query.
Delegate | Return Type | Description |
|---|---|---|
|
| A task that completes when the search operation finishes. |
SearchDelegate parameters
Parameter | Type | Description |
|---|---|---|
|
| The query text supplied by the search box. |
|
| Reports determinate progress from |
|
| A token that is canceled when the current search should stop. |
SearchBoxViewModel
Provides search input state and invokes an application-provided SearchDelegate when the query changes.
SearchBoxViewModel constructors
Constructor | Description |
|---|---|
| Initializes a new instance of the |
| Initializes a new instance of the |
SearchBoxViewModel(string typeId, ILoggerFactory loggerFactory, SearchDelegate searchCallback, TimeSpan? throttleTime = null)
Parameter | Type | Description |
|---|---|---|
|
| The type identifier used to build the navigation id. |
|
| The logger factory used to report callback failures. |
|
| The callback that applies each search query. |
|
| The optional debounce interval for text changes. When |
Property | Type | Description |
|---|---|---|
|
| Gets the historical property that stores the current query text. |
|
| Gets the idle-state flag, which is |
|
| Gets a value indicating whether the search callback is currently running. |
|
| Gets the current progress value. |
|
| Gets the command that reruns the current query. |
Method | Return Type | Description |
|---|---|---|
|
| Cancels the current search and restores the idle state. |
|
| Starts the current query again without applying the debounce interval. |
|
| Sets the query text to an empty string. |
|
| Clears the query unless the supplied token is already canceled. |
|
| Returns the historical query property as the child view model. |
|
| Focuses the search input and continues normal child navigation. |
|
| Starts the specified query immediately. |
|
| Starts the specified query immediately with caller-controlled cancellation. |
|
| Requests focus for the query text and its bound input. |
|
| Updates the current search progress. |
SearchBoxViewModel.Refresh
Parameter | Type | Description |
|---|---|---|
|
| A token linked to the started search operation. |
SearchBoxViewModel.ClearCommandCall
Parameter | Type | Description |
|---|---|---|
|
| A token that prevents the clear operation when already canceled. |
SearchBoxViewModel.Navigate
Parameter | Type | Description |
|---|---|---|
|
| The navigation id passed to the base implementation. |
|
| A token that cancels navigation. |
SearchBoxViewModel.Query(string? text)
Parameter | Type | Description |
|---|---|---|
|
| The query to start. |
SearchBoxViewModel.Query(string? text, CancellationToken cancel)
Parameter | Type | Description |
|---|---|---|
|
| The query to start. |
|
| A token linked to the started search operation. |
SearchBoxViewModel.Report
Parameter | Type | Description |
|---|---|---|
|
| The progress value exposed through |