Nello script 17 abbiamo introdotto l'utilizzo della charm di ricerca come strumento attraverso cui immettere del testo per cercare informazioni in una app.
La charm di ricerca supporta anche la possibilità di visualizzare una serie di chiavi di ricerca suggerite, relative in qualche modo al testo immesso dall'utente; possiamo implementare anche nelle nostre app questa funzionalità registrandosi all'evento SuggestionsRequested di SearchPane.
Tale evento si scatena al cambiamento del testo di ricerca, il momento corretto in cui registrarsi è nell'override di OnWindowCreated direttamente nella classe App.
protected override void OnWindowCreated(WindowCreatedEventArgs wargs) { base.OnWindowCreated(wargs); var searchPane = SearchPane.GetForCurrentView(); searchPane.SuggestionsRequested += async (SearchPane sender, SearchPaneSuggestionsRequestedEventArgs args) => { string[] suggestions = { "AAA", "AAABBB", "AAACCC", "AAADDD", "EEE", "FFF", "GGG", "HHH", "III", "JJJ", "KKK", "LLL", "MMM", "NNN", "OOO", "PPP", "QQQ" }; foreach (string suggestion in suggestions) { if (suggestion.StartsWith(args.QueryText, StringComparison.CurrentCultureIgnoreCase)) { args.Request.SearchSuggestionCollection.AppendQuerySuggestion(suggestion); } if (args.Request.SearchSuggestionCollection.Size >= 5) break; } }; }
Con:
var searchPane = SearchPane.GetForCurrentView();
recuperiamo il riferimento, per così dire, alla charm. Attraverso SearchPaneSuggestionsRequestedEventArgs possiamo recuperare il testo immesso dall'utente e soprattutto possiamo aggiungere alla collezione SearchSuggestionCollection l'occorrenza del 'suggerimento' relativo con il metodo AppendQuerySuggestion.
Nell'esempio la collezione delle chiavi 'suggeribili' è cablato nel codice così come la logica di check è un semplice StartsWith, è molto più realistico che questi siano esposti attraverso un servizio e che esso possa aggiungere altre logiche di relazione.
Per questo scenario dobbiamo adattare leggermente il codice al modello asincrono dell'accesso ad un servizio, attraverso il metodo GetDeferral:
protected override void OnWindowCreated(WindowCreatedEventArgs wargs) { base.OnWindowCreated(wargs); var searchPane = SearchPane.GetForCurrentView(); searchPane.SuggestionsRequested += async (SearchPane sender, SearchPaneSuggestionsRequestedEventArgs args) => { var deferral = args.Request.GetDeferral(); try { string[] suggestions = await DataService.GetSuggestions(); foreach (string suggestion in suggestions) { if (suggestion.StartsWith(args.QueryText, StringComparison.CurrentCultureIgnoreCase)) { args.Request.SearchSuggestionCollection.AppendQuerySuggestion(suggestion); } if (args.Request.SearchSuggestionCollection.Size >= 5) break; } } finally { deferral.Complete(); } }; } public class DataService { public static async Task<string[]> GetSuggestions() { await Task.Delay(3000); string[] suggestions = { "AAA", "AAABBB", "AAACCC", "AAADDD", "EEE", "FFF", "GGG", "HHH", "III", "JJJ", "KKK", "LLL", "MMM", "NNN", "OOO", "PPP", "QQQ" }; return suggestions; } }
L'ultima accortezza da avere è richiamare il metodo deferral.Complete(); al termine del processo.
Commenti
Per inserire un commento, devi avere un account.
Fai il login e torna a questa pagina, oppure registrati alla nostra community.
Approfondimenti
Creare Azure Function con supporto a OpenAPI
Introduzione a Azure Container Apps
Compilare automaticamente applicazioni .NET 6 con le pipeline di Azure DevOps e GitHub Action
Impostare il tema light o dark utilizzando i CSS
Utilizzare la parola chiave var con lambda eExpression e method group in C# 10
Scaling per app con Azure App Service
Parallelizzare le chiamate HTTP con async/await e le Promise in JavaScript
Creare l'effetto floating label per gli input con Bootstrap 5
Creare un job summary in una GitHub Action
Indicizzare e ricercare i blob di Azure Storage
Utilizzare Front Door come CDN di contenuti statici
Leggere il valore di un header della richiesta in ASP.NET Core 6
I più letti di oggi
- ecco tutte le novità pubblicate sui nostri siti questa settimana: https://aspit.co/wkly buon week-end!
- Gestire form con più di un pulsante submit in ASP.NET MVC
- Usare TryUpdateModelAsync per aggiornare le entity nelle action di POST in ASP.NET Core MVC
- ecco tutte le novità pubblicate sui nostri siti questa settimana: https://aspit.co/wkly buon week-end!
- ecco tutte le novità pubblicate sui nostri siti questa settimana: https://aspit.co/wkly buon week-end!
- ecco tutte le novità pubblicate sui nostri siti questa settimana: https://aspit.co/wkly buon week-end!