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
Utilizzare Copilot con Azure Cosmos DB
Generare una User Delegation SAS in .NET per Azure Blob Storage
Utilizzare Azure Cosmos DB con i vettori
Configurare e gestire sidecar container in Azure App Service
Potenziare la ricerca su Cosmos DB con Full Text Search
Eseguire una ricerca avanzata per recuperare le issue di GitHub
Ottimizzare le pull con Artifact Cache di Azure Container Registry
Path addizionali per gli asset in ASP.NET Core MVC
Gestione degli eventi nei Web component HTML
Gestione file Javascript in Blazor con .NET 9
Creare agenti facilmente con Azure AI Agent Service
Utilizzare la funzione EF.Parameter per forzare la parametrizzazione di una costante con Entity Framework
I più letti di oggi
- ecco tutte le novità pubblicate sui nostri siti questa settimana: https://aspit.co/wkly buon week-end!
- Utilizzare il pattern matching per semplificare le espressioni
- ecco tutte le novità pubblicate sui nostri siti questa settimana: https://aspit.co/wkly buon week-end!
- Utilizzare requestAnimationFrame per animazioni fluide
- 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!