Searchbox throws up an error for listview Xamarin.Forms

122
August 29, 2021, at 06:40 AM

Trying to make a searchbox for my listview, but it throws up an error, i know why it throws up the code, because it's using ScanResult, which is not a string and is using multiple strings combined, can't figure out which handle i should use for the search bar as i tried all of them and every one of them were giving errors, it gives the error: cannot convert from 'string' to 'svitrkods.Views.ScanResult' xaml.cs

namespace svitrkods.Views
{
    public class ScanResult
    {
        public string Data { get; set; }
        public string Symbology { get; set; }
    }
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class Page1 : ContentPage
    {
        private ObservableCollection<ScanResult> _scanResults;
        public ObservableCollection<ScanResult> ScanResults
        {
            get { return _scanResults ?? (_scanResults = new ObservableCollection<ScanResult>()); }
        }
        public Page1()
        {
            InitializeComponent();
            ResultsList.ItemsSource = ScanResults;
        }
        public void AddResult(string data, string symbology)
        {
            ScanResults.Add(new ScanResult()
            {
                Data = data,
                Symbology = symbology
            });
            ResultsList.Header = string.Format("Rezultāti ({0})", ScanResults.Count);
        }
        private void Scan_Button(object sender, EventArgs e)
        {
            Navigation.PopModalAsync();
        }
        private void SearchBar_Pressed(object sender, EventArgs e)
        {
            var keyword = SearchBar.Text;
            ResultsList.ItemsSource = ScanResults.Where(data => ScanResults.Contains(keyword));
        }
    }
}

XAML

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="svitrkods.Views.Page1"
    xmlns:vm="clr-namespace:svitrkods.ViewModels"
             Title="Scan Results">
    <ContentPage.Content>
        <AbsoluteLayout>
            <ListView
                      x:Name="ResultsList"
                      Header="Rezultāti (0)"
                      AbsoluteLayout.LayoutBounds="0,0,1,1"
                      AbsoluteLayout.LayoutFlags="All"
                      Background="White"
                      HorizontalScrollBarVisibility="Never"
                      VerticalScrollBarVisibility="Default"
                      SelectionMode="None"
                      SeparatorVisibility="Default"
                      HasUnevenRows="True"
                      VerticalOptions="StartAndExpand">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <StackLayout Orientation="Vertical" Padding="15, 15, 15, 5">
                                <Label Text="{Binding Data}" FontSize="Medium" TextColor="Black" />
                                <Label Text="{Binding Symbology}" FontSize="Small" TextColor="#58B5C2" />
                            </StackLayout>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
            <SearchBar x:Name="SearchBar" 
           SearchButtonPressed="SearchBar_Pressed"
           Placeholder="Atrodi svītrkodu..."
           CancelButtonColor="#58B5C2"
           PlaceholderColor="#58B5C2"
           TextColor="#58B5C2"
           HorizontalTextAlignment="Center"
           AbsoluteLayout.LayoutBounds="0.5,0,300,25"
           AbsoluteLayout.LayoutFlags="PositionProportional"
           FontSize="Medium"
           FontAttributes="Italic" />
            <Button Text="Atpakaļ uz skeneri"
                    Clicked="Scan_Button"
                    WidthRequest="300"
                    AbsoluteLayout.LayoutBounds="0,0.9,1,0.1"
                    AbsoluteLayout.LayoutFlags="All"
                    BackgroundColor="#58B5C2"
                    TextColor="White"
                    HorizontalOptions="Center" />
        </AbsoluteLayout>
    </ContentPage.Content>
</ContentPage>
Answer 1

this line

ResultsList.ItemsSource = ScanResults.Where(data => ScanResults.Contains(keyword));

should be

ResultsList.ItemsSource = ScanResults.Where(r => r.data.Contains(keyword)).ToList();
Rent Charter Buses Company
READ ALSO
Change type / len of a user defined column in My SQL Workbench

Change type / len of a user defined column in My SQL Workbench

I am using a MySQL query in Workbench that uses group_concat() to group multiple string values

109
Is there a way to bypass anonymising service check?

Is there a way to bypass anonymising service check?

I have been dabbling with python and selenium for a while nowI Was trying to work with a website, and as I realize that I may have to make multiple calls to the site, and to avoid any ip related issue, I tried to use proxy

108
Adding swatches to a mail template without using a layout

Adding swatches to a mail template without using a layout

I am working on a mail template and I would like to ask if it's possible to add the colour swatches functionality to aphtml

128