What is a "DataService" In this context

Im using .net 8 Maui, and I don't know what this article is trying to get when it's asking for a DataService. I copied the code from the article because it seems that it is showing you how to get specific keywords for a search, but the only thing keeping the given code from working is the DataService class, I'm not sure how to make it. Here's the article I used: https://learn.microsoft.com/en-us/dotnet/maui/user-interface/controls/searchbar?view=net-maui-8.0
SearchBar - .NET MAUI
The .NET MAUI SearchBar is a user input control that is used for initiating a search. The SearchBar control supports placeholder text, query input, execution, and cancellation.
17 Replies
leowest
leowest2mo ago
@CreamofSumYungGai DataService is essentially something like this
public class DataService
{
private readonly SearchContext _searchContext;
public DataService(SearchContext searchContext)
{
_searchContext = searchContext;
}

public List<string> GetSearchResults(string term)
{
return _searchContext.Fruits.Where(x => x.Name == term).ToList();
}
}
public class DataService
{
private readonly SearchContext _searchContext;
public DataService(SearchContext searchContext)
{
_searchContext = searchContext;
}

public List<string> GetSearchResults(string term)
{
return _searchContext.Fruits.Where(x => x.Name == term).ToList();
}
}
And in this sample SearchContext is EFCore wired to some database. that is just an example, the idea here of what its trying to say is DataService can retrieve information of some db or list or file. to the viewmodel
CreamofSumYungGai
Can I send you my xaml so you can see what I want it to retrieve? I essentially just want the typed characters in the search box to return results that contain those characters
<SearchBar
MaxLength="500"
Placeholder="Search your notes here"
SearchCommand="{Binding SearchButtonPressed}"
SearchCommandParameter=""
TextChanged="OnTextChanged"
WidthRequest="1050" />
<ListView x:Name="searchResults" />
<SearchBar
MaxLength="500"
Placeholder="Search your notes here"
SearchCommand="{Binding SearchButtonPressed}"
SearchCommandParameter=""
TextChanged="OnTextChanged"
WidthRequest="1050" />
<ListView x:Name="searchResults" />
leowest
leowest2mo ago
and what is the result and the certain characters
CreamofSumYungGai
I want it to detect any sort of character and see if it matches up in the existing button's titles the buttons are notes
CreamofSumYungGai
Not sure where to keep going with this
No description
leowest
leowest2mo ago
ok and do u have some mapping of those buttons and tiles? how are u placing it using xaml?
CreamofSumYungGai
Do you want me to show you the UI layout? How does it get the search context from my search box though? I want it to get the characters typed then return whatever results contain those characters
leowest
leowest2mo ago
from the searchBar.Text u have 2 events wired to your searchbar first one is button pressed second one is when it changes SearchCommand="{Binding SearchButtonPressed}" TextChanged="OnTextChanged" with the second one when something changes, it fires the method OnTextChanged in your code behind, from there u can get a handle to the control SearchBar this casts the sender to a type of SearchBar if it matches then u can access the control
var searchBar = (SearchBar)sender;
var searchBar = (SearchBar)sender;
Then searchBar.Text lets u see what has been typed by the user the problem is how your button's titles the buttons are notes are laid in your XAML because depending how they are u can easily return the right one based on the search or if they are just a List in your code behind sorry I was busy to provide a full answer earlier
CreamofSumYungGai
It's ok Ok, so what should I do with the searchBar.text in this case? Should it be the data service? I just need to access it with the .GetSearchResults
leowest
leowest2mo ago
searchBar.Text is a string with whatever people are typing on the visual textbox u see u are already acessing it on the event OnTextChanged I am unsure what is unclear here. DataService is just means to retrieve data from a place X could even be as simple as a List<string>
CreamofSumYungGai
Could you give me a snippet of code if possible? So add this to the code and it'll be good right? I'm just missing an assembly for data service so I'm assuming what you pointed out is the fix
leowest
leowest2mo ago
I strongly suggest you go thru the basics first so you understand what is going on https://learn.microsoft.com/en-us/shows/csharp-fundamentals-for-absolute-beginners/ and use console app before u take the bigger step to use a ui specially MAUI
C# Fundamentals for Absolute Beginners
Learn C# programming from an expert in the industry. Get the tools, see how to write code, debug features, explore customizations, and more. For newer videos head over to dot.net/videos
CreamofSumYungGai
Does the snippet work I just need the search query to work please 😓 No need to send me the beginner article
leowest
leowest2mo ago
this is not a "we write code for you discord", as much as I would like to point u in the right direction u have no idea what is going on and just wants code given to u, make some effort.
CreamofSumYungGai
Sure, I’ll test out the code given your information then update you
leowest
leowest2mo ago
ok and do go thru the link above so u understand at least the basics of what is going on so u can use the information I have provided u
CreamofSumYungGai
Alr