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
Eseguire un metodo asincrono dopo il set di una proprietà in Blazor 8
Utilizzare il nuovo modello GPT-4o con Azure OpenAI
Recuperare automaticamente un utente e aggiungerlo ad un gruppo di Azure DevOps
Potenziare la ricerca su Cosmos DB con Full Text Search
Miglioramenti nelle performance di Angular 16
Creare una libreria CSS universale - Rotazione degli elementi
Path addizionali per gli asset in ASP.NET Core MVC
Creare un webhook in Azure DevOps
Garantire la provenienza e l'integrità degli artefatti prodotti su GitHub
Eseguire query manipolando liste di tipi semplici con Entity Framework Core
Migliorare l'organizzazione delle risorse con Azure Policy
Collegare applicazioni server e client con .NET Aspire