首页 > 解决方案 > 如何解决“System.NullReferenceException:'对象引用未设置为对象的实例。'”

问题描述

我知道这个问题被问了几百万次,但我只是不知道如何解决这个问题。

我有一个带有列表视图的页面。我正在尝试实现搜索栏,但此时我在 xaml.cs 文件中出现异常

**ListViewData.ItemsSource = searchResult;**

谁能帮助并解释如何解决这个问题?我已经尝试了太多的选择在这里提到它..

在 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"
             xmlns:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d"
             x:Class="pytaniaApp.ShowDataPage" >
   
    <StackLayout x:Name="stackLayout">
        <SearchBar Placeholder="Search" 
                   x:Name="DataSearch"
                   SearchButtonPressed="DataSearch_SearchButtonPressed"/>

        <Label Text="Kliknij zeby zmienic lub usunac wpis"
           VerticalOptions="Center"
           HorizontalOptions="Center"
           FontSize="Large"/>
        
        <ListView x:Name="ListViewData"             
              ItemSelected="OnSelectedItem"
                  HorizontalScrollBarVisibility="Always"
                  RowHeight="40">
           <ListView.ItemTemplate>                
                <DataTemplate>
                    <ViewCell>
                        <StackLayout>                            
                            <Label Text="{Binding DataLabel}"
                                   LineBreakMode="WordWrap"/>                            
                        </StackLayout>
                        </ViewCell> 
                        </DataTemplate>
            </ListView.ItemTemplate>            
        </ListView>
    </StackLayout>
   
</ContentPage>

xaml.cs 文件的代码:

using SQLite;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace pytaniaApp
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class ShowDataPage : ContentPage
    {
        
        string dbPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "database.db3");

        public ShowDataPage()
        {
            InitializeComponent();
                            
        }
        
        protected override void OnAppearing()
        {
            var db = new SQLiteConnection(dbPath);
            ListViewData.ItemsSource = db.Table<Data>().OrderBy(x => x.Date).ToList();

            BindingContext = new Data();
            

            base.OnAppearing();

        }

        public async void OnSelectedItem(object sender, SelectedItemChangedEventArgs e)
        {
            var myitem = e.SelectedItem as Data;

            await Navigation.PushAsync(new PageEditEntry(myitem.Id, myitem.Date, myitem.Hours));
        }
        
        private void DataSearch_SearchButtonPressed(object sender, EventArgs e)
        {
            var db = new SQLiteConnection(dbPath);
            string keyword = DataSearch.Text;
      
                
             var searchResult = db.Table<Data>().Where(x => x.DataLabel.Contains(keyword));
            
            ListViewData.ItemsSource = searchResult;
        }
    }
}

以及Data.cs的代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Text;
using SQLite;

namespace pytaniaApp
{

    public class Data : INotifyPropertyChanged

    {
        public event PropertyChangedEventHandler PropertyChanged;

        void OnPropertyChanged([CallerMemberName] string name = "")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
        }

        [PrimaryKey, AutoIncrement]
        public int Id
        {
            get;
            set;
        }
        public double Hours
        {
            get;
            set;
        }
       
        public DateTime Date

        {
            get;
            set;
        }

        public override string ToString()
        {

            double hours_rounded = Math.Truncate(Hours);
            double minutes_left = (Hours - hours_rounded) * 60;
            return "W dniu " + this.Date.ToString("dd-MM-yyyy") + " przepracowalas " + hours_rounded + " godzin(y) i " + minutes_left + " minut.";
        }

        public string DataLabel
        {
            get { return ToString(); }

        }



    }
}

在此处输入图像描述

标签: c#sqlitexamarin.formssearchbar

解决方案


推荐阅读