c# - 如何解决“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(); }
}
}
}
解决方案
推荐阅读
- mysql - 'on 子句'中的未知列 - 尝试了 where 和有条件但同样的错误
- javascript - Shiny renderUI 完成后回调
- javascript - 读取 DOM(网页)中的位置并相应地更改导航菜单类
- rest - 容器 (Kubernetes) 与 Web 服务 (REST API)
- c++ - 如何让 HC-06 模块向我的 Android 设备发送信号?
- javascript - 如何从javascript中的字符串中获取两个字符之间的最后一个字符?
- postgresql - postgresql 上的表缺少 FROM 子句条目
- bash - 如何修改 git log pretty 格式以从其中一个字段中删除某些字符(如逗号)?
- nestjs - 如何在 Nest.js 中使用 grpc 原型类型 Struct
- omnet++ - 需要 c++ 函数,如单或多无线电无线节点接口的处理消息函数