如何定位抛出“NullReferenceException: 'Object reference not set to an instance of an object'的方法?
How to locate the method that throws "NullReferenceException: 'Object reference not set to an instance of an object'?
我已经在 Whosebug 上检查了所有类似的问题。 None 他们要么回答了,要么真的问了同样的问题。
有没有办法找到试图引用所述空对象的方法?
我知道它不能说出是什么对象,因为它不知道,但是它必须知道什么方法正在尝试空引用,但不会显示它。在调试模式下,代码屏幕显示:
"Your app has entered a break state, but there is no code to show because all threads were executing external code (typically system or framework code)."
我点击继续执行以读取输出并从那里找到它,但是输出中没有任何相关内容:
**System.NullReferenceException:** 'Loading...'
Thread finished: <Thread Pool> #4
The thread 0x4 has exited with code 0 (0x0).
Thread finished: <Thread Pool> #5
The thread 0x5 has exited with code 0 (0x0).
Thread finished: <Thread Pool> #2
The thread 0x2 has exited with code 0 (0x0).
Thread finished: <Thread Pool> #3
The thread 0x3 has exited with code 0 (0x0).
Thread finished: <Thread Pool> #6
The thread 0x6 has exited with code 0 (0x0).
Thread started: <Thread Pool> #7
Thread started: <Thread Pool> #8
Thread started: <Thread Pool> #9
11-06 14:17:23.932 D/Mono ( 5445): DllImport attempting to load: '/system/lib/liblog.so'.
11-06 14:17:23.933 D/Mono ( 5445): DllImport loaded library '/system/lib/liblog.so'.
11-06 14:17:23.933 D/Mono ( 5445): DllImport searching in: '/system/lib/liblog.so' ('/system/lib/liblog.so').
11-06 14:17:23.933 D/Mono ( 5445): Searching for '__android_log_print'.
11-06 14:17:23.933 D/Mono ( 5445): Probing '__android_log_print'.
11-06 14:17:23.933 D/Mono ( 5445): Found as '__android_log_print'.
11-06 14:17:23.936 I/MonoDroid( 5445): UNHANDLED EXCEPTION:
11-06 14:17:23.938 I/MonoDroid( 5445): System.NullReferenceException: Object reference not set to an instance of an object.
11-06 14:17:23.938 I/MonoDroid( 5445): at Xamarin.Forms.Grid.NumberOfUnsetRowHeight (Xamarin.Forms.BindableObject child) [0x00014] in D:\a\s\Xamarin.Forms.Core\GridCalc.cs:627
11-06 14:17:23.938 I/MonoDroid( 5445): at Xamarin.Forms.Grid.MeasureStarredRows () [0x0007b] in D:\a\s\Xamarin.Forms.Core\GridCalc.cs:545
11-06 14:17:23.938 I/MonoDroid( 5445): at Xamarin.Forms.Grid.MeasureAndContractStarredRows (System.Double width, System.Double height, System.Double totalStarsHeight) [0x00000] in D:\a\s\Xamarin.Forms.Core\GridCalc.cs:405
11-06 14:17:23.938 I/MonoDroid( 5445): at Xamarin.Forms.Grid.MeasureGrid (System.Double width, System.Double height, System.Boolean requestSize) [0x00117] in D:\a\s\Xamarin.Forms.Core\GridCalc.cs:512
11-06 14:17:23.938 I/MonoDroid( 5445): at Xamarin.Forms.Grid.OnSizeRequest (System.Double widthConstraint, System.Double heightConstraint) [0x0002a] in D:\a\s\Xamarin.Forms.Core\GridCalc.cs:60
11-06 14:17:23.938 I/MonoDroid( 5445): at Xamarin.Forms.VisualElement.OnMeasure (System.Double widthConstraint, System.Double heightConstraint) [0x00000] in D:\a\s\Xamarin.Forms.Core\VisualElement.cs:762
11-06 14:17:23.938 I/MonoDroid( 5445): at Xamarin.Forms.VisualElement.GetSizeRequest (System.Double widthConstraint, System.Double heightConstraint) [0x00053] in D:\a\s\Xamarin.Forms.Core\VisualElement.cs:644
11-06 14:17:23.939 I/MonoDroid( 5445): at Xamarin.Forms.Layout.GetSizeRequest (System.Double widthConstraint, System.Double heightConstraint) [0x00000] in D:\a\s\Xamarin.Forms.Core\Layout.cs:132
11-06 14:17:23.939 I/MonoDroid( 5445): at Xamarin.Forms.VisualElement.Measure (System.Double widthConstraint, System.Double heightConstraint, Xamarin.Forms.MeasureFlags flags) [0x00054] in D:\a\s\Xamarin.Forms.Core\VisualElement.cs:702
11-06 14:17:23.939 I/MonoDroid( 5445): at Xamarin.Forms.StackLayout.CalculateNaiveLayout (Xamarin.Forms.StackLayout+LayoutInformation layout, Xamarin.Forms.StackOrientation orientation, System.Double x, System.Double y, System.Double widthConstraint, System.Double heightConstraint) [0x000a8] in D:\a\s\Xamarin.Forms.Core\StackLayout.cs:163
11-06 14:17:23.939 I/MonoDroid( 5445): at Xamarin.Forms.StackLayout.CalculateLayout (Xamarin.Forms.StackLayout+LayoutInformation layout, System.Double x, System.Double y, System.Double widthConstraint, System.Double heightConstraint, System.Boolean processExpanders) [0x00058] in D:\a\s\Xamarin.Forms.Core\StackLayout.cs:123
11-06 14:17:23.939 I/MonoDroid( 5445): at Xamarin.Forms.StackLayout.LayoutChildren (System.Double x, System.Double y, System.Double width, System.Double height) [0x0004e] in D:\a\s\Xamarin.Forms.Core\StackLayout.cs:58
11-06 14:17:23.939 I/MonoDroid( 5445): at Xamarin.Forms.Layout.UpdateChildrenLayout () [0x00158] in D:\a\s\Xamarin.Forms.Core\Layout.cs:266
11-06 14:17:23.939 I/MonoDroid( 5445): at Xamarin.Forms.Layout.OnSizeAllocated (System.Double width, System.Double height) [0x0000f] in D:\a\s\Xamarin.Forms.Core\Layout.cs:224
11-06 14:17:23.939 I/MonoDroid( 5445): at Xamarin.Forms.VisualElement.SizeAllocated (System.Double width, System.Double height) [0x00000] in D:\a\s\Xamarin.Forms.Core\VisualElement.cs:784
11-06 14:17:23.939 I/MonoDroid( 5445): at Xamarin.Forms.Layout.ForceLayout () [0x00000] in D:\a\s\Xamarin.Forms.Core\Layout.cs:125
11-06 14:17:23.939 I/MonoDroid( 5445): at Xamarin.Forms.Platform.Android.ViewCellRenderer+ViewCellContainer.Update (Xamarin.Forms.ViewCell cell) [0x00185] in D:\a\s\Xamarin.Forms.Platform.Android\Cells\ViewCellRenderer.cs:178
11-06 14:17:23.939 I/MonoDroid( 5445): at Xamarin.Forms.Platform.Android.ViewCellRenderer.GetCellCore (Xamarin.Forms.Cell item, Android.Views.View convertView, Android.Views.ViewGroup parent, Android.Content.Context context) [0x00027] in D:\a\s\Xamarin.Forms.Platform.Android\Cells\ViewCellRenderer.cs:21
11-06 14:17:23.939 I/MonoDroid( 5445): at Xamarin.Forms.Platform.Android.CellRenderer.GetCell (Xamarin.Forms.Cell item, Android.Views.View convertView, Android.Views.ViewGroup parent, Android.Content.Context context) [0x00075] in D:\a\s\Xamarin.Forms.Platform.Android\Cells\CellRenderer.cs:51
11-06 14:17:23.939 I/MonoDroid( 5445): at Xamarin.Forms.Platform.Android.CellFactory.GetCell (Xamarin.Forms.Cell item, Android.Views.View convertView, Android.Views.ViewGroup parent, Android.Content.Context context, Xamarin.Forms.View view) [0x0001e] in D:\a\s\Xamarin.Forms.Platform.Android\Cells\CellFactory.cs:20
11-06 14:17:23.939 I/MonoDroid( 5445): at Xamarin.Forms.Platform.Android.ListViewAdapter.GetView (System.Int32 position, Android.Views.View convertView, Android.Views.ViewGroup parent) [0x00200] in D:\a\s\Xamarin.Forms.Platform.Android\Renderers\ListViewAdapter.cs:299
11-06 14:17:23.939 I/MonoDroid( 5445): at Android.Widget.BaseAdapter.n_GetView_ILandroid_view_View_Landroid_view_ViewGroup_ (System.IntPtr jnienv, System.IntPtr native__this, System.Int32 position, System.IntPtr native_convertView, System.IntPtr native_parent) [0x0001a] in <11a340ccc8de43f09c97400139266ef5>:0
11-06 14:17:23.939 I/MonoDroid( 5445): at (wrapper dynamic-method) Android.Runtime.DynamicMethodNameCounter.68(intptr,intptr,int,intptr,intptr)
11-06 14:17:23.950 W/.TheStylingRoo( 5445): JNI RegisterNativeMethods: attempt to register 0 native methods for android.runtime.JavaProxyThrowable
11-06 14:17:23.954 D/Mono ( 5445): DllImport searching in: '__Internal' ('(null)').
11-06 14:17:23.955 D/Mono ( 5445): Searching for 'java_interop_jnienv_throw'.
11-06 14:17:23.955 D/Mono ( 5445): Probing 'java_interop_jnienv_throw'.
11-06 14:17:23.955 D/Mono ( 5445): Found as 'java_interop_jnienv_throw'.
**System.NullReferenceException:** 'Object reference not set to an instance of an object.'
这个让我难住了。当尝试加载一个显示客户列表的页面和另一个加载预订的页面时,会触发异常。请注意,启动页面具有相同的逻辑来加载当前预订列表,并且工作正常。
我搜索了所有相关问题并尝试了以下解决方案:
- 单步执行代码。它在 2 个页面构造函数中的任何一个结束时触发,不包括启动页面构造函数。这些构造函数中的所有方法都可以正常执行。
- 正在寻找空值。没有对象或属性为空。
- 正在发表评论,尝试捕捉和 if !'s。 None 这三个防止异常。
- 正在重新创建 xaml 事件。我确定是这个,但不是。
- 正在查看异常详细信息。异常弹出窗口根本没有详细信息。
- 更改异步方法。显示预订的启动视图实际上是一个副本,包括异步方法。它工作正常。
- 正在删除绑定。
- Android 模拟器的清理、重建、出厂重置。各种顺序
- 正在重启Visual Studio
- 正在删除 bin 文件夹并重新加载
相关代码:
MainPage.xaml.cs(只是加载 ClientsPage 的事件)
private async void Button_Clicked(object sender, EventArgs e)
{
try
{
await Navigation.PushAsync(new Clients_Page());
}
catch(Exception ex)
{
Console.WriteLine(ex);
}
}
Clients_Page.xaml.cs(只是构造函数)
public partial class Clients_Page : ContentPage
{
public Clients_Page()
{
InitializeComponent();
Clients_ViewModel viewModel = new Clients_ViewModel();
viewModel.LoadClients();
BindingContext = viewModel;
// Commenting all but InitializeComponent (Including Bindings in xaml) has no effect, so the ViewModel is not to blame
} // EXCEPTION FIRES AFTER THIS LINE
Clients_ViewModel.cs
class Clients_ViewModel : INotifyPropertyChanged
{
//PROPERTIES_________________________________________________________
public event PropertyChangedEventHandler PropertyChanged;
private IRepository database { get; }
private ObservableCollection<Client> clients;
public ObservableCollection<Client> Clients
{
get { return clients; }
set
{
if (value != clients)
{
clients = value;
if (PropertyChanged != null)
{
NotifyPropertyChanged("Clients");
}
}
}
}
public async void LoadClients()
{
Clients = new ObservableCollection<Client>(await database.GetAllClients());
}
private void NotifyPropertyChanged(String propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (null != handler)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
public Clients_ViewModel()
{
database = App.Database_IRepository;
}
}//endclass
}//endnamespace
Repository.cs(仅 GetAllClients 任务)
public async Task<List<Client>> GetAllClients()
{
return await database.Table<Client>().OrderBy(c => c.FirstName).ToListAsync();
}
Xaml 只是一个使用 ItemsSource="{Binding Clients}" 的 ListView 和一个使用 Text="{Binding FirstName}" 的 ViewCell"
我精通调试,并且会很高兴找到具有空对象引用的方法。我只需要知道如何让调试器显示所述方法。
更新 1:即使删除了 xaml 中的所有绑定并且删除了对视图模型的调用,也会触发异常。我也试过评论整个 Clients_Page.xaml 除了 ContentPage 和主网格,错误仍然存在。所以它不能是 ViewModel 或 xaml
更新 2:即使更改点击事件以导航到新页面也会出错
已解决
我不得不删除 NavigationPage.SetHasNavigationBar(this, false);来自 Main_Page.xaml.cs 构造函数(完整块如下所示)
public Main_Page()
{
InitializeComponent();
//NavigationPage.SetHasNavigationBar(this, false);
Main_ViewModel viewModel = new Main_ViewModel();
viewModel.LoadCurrentBookings();
BindingContext = viewModel;
}
我添加该行是为了在我的应用 运行 时删除难看的 Android 导航栏,但是 Android 页面导航似乎以某种方式链接到该栏,所以我的启动视图无法导航到任何其他设置为 false
的视图
我已经在 Whosebug 上检查了所有类似的问题。 None 他们要么回答了,要么真的问了同样的问题。
有没有办法找到试图引用所述空对象的方法?
我知道它不能说出是什么对象,因为它不知道,但是它必须知道什么方法正在尝试空引用,但不会显示它。在调试模式下,代码屏幕显示:
"Your app has entered a break state, but there is no code to show because all threads were executing external code (typically system or framework code)."
我点击继续执行以读取输出并从那里找到它,但是输出中没有任何相关内容:
**System.NullReferenceException:** 'Loading...'
Thread finished: <Thread Pool> #4
The thread 0x4 has exited with code 0 (0x0).
Thread finished: <Thread Pool> #5
The thread 0x5 has exited with code 0 (0x0).
Thread finished: <Thread Pool> #2
The thread 0x2 has exited with code 0 (0x0).
Thread finished: <Thread Pool> #3
The thread 0x3 has exited with code 0 (0x0).
Thread finished: <Thread Pool> #6
The thread 0x6 has exited with code 0 (0x0).
Thread started: <Thread Pool> #7
Thread started: <Thread Pool> #8
Thread started: <Thread Pool> #9
11-06 14:17:23.932 D/Mono ( 5445): DllImport attempting to load: '/system/lib/liblog.so'.
11-06 14:17:23.933 D/Mono ( 5445): DllImport loaded library '/system/lib/liblog.so'.
11-06 14:17:23.933 D/Mono ( 5445): DllImport searching in: '/system/lib/liblog.so' ('/system/lib/liblog.so').
11-06 14:17:23.933 D/Mono ( 5445): Searching for '__android_log_print'.
11-06 14:17:23.933 D/Mono ( 5445): Probing '__android_log_print'.
11-06 14:17:23.933 D/Mono ( 5445): Found as '__android_log_print'.
11-06 14:17:23.936 I/MonoDroid( 5445): UNHANDLED EXCEPTION:
11-06 14:17:23.938 I/MonoDroid( 5445): System.NullReferenceException: Object reference not set to an instance of an object.
11-06 14:17:23.938 I/MonoDroid( 5445): at Xamarin.Forms.Grid.NumberOfUnsetRowHeight (Xamarin.Forms.BindableObject child) [0x00014] in D:\a\s\Xamarin.Forms.Core\GridCalc.cs:627
11-06 14:17:23.938 I/MonoDroid( 5445): at Xamarin.Forms.Grid.MeasureStarredRows () [0x0007b] in D:\a\s\Xamarin.Forms.Core\GridCalc.cs:545
11-06 14:17:23.938 I/MonoDroid( 5445): at Xamarin.Forms.Grid.MeasureAndContractStarredRows (System.Double width, System.Double height, System.Double totalStarsHeight) [0x00000] in D:\a\s\Xamarin.Forms.Core\GridCalc.cs:405
11-06 14:17:23.938 I/MonoDroid( 5445): at Xamarin.Forms.Grid.MeasureGrid (System.Double width, System.Double height, System.Boolean requestSize) [0x00117] in D:\a\s\Xamarin.Forms.Core\GridCalc.cs:512
11-06 14:17:23.938 I/MonoDroid( 5445): at Xamarin.Forms.Grid.OnSizeRequest (System.Double widthConstraint, System.Double heightConstraint) [0x0002a] in D:\a\s\Xamarin.Forms.Core\GridCalc.cs:60
11-06 14:17:23.938 I/MonoDroid( 5445): at Xamarin.Forms.VisualElement.OnMeasure (System.Double widthConstraint, System.Double heightConstraint) [0x00000] in D:\a\s\Xamarin.Forms.Core\VisualElement.cs:762
11-06 14:17:23.938 I/MonoDroid( 5445): at Xamarin.Forms.VisualElement.GetSizeRequest (System.Double widthConstraint, System.Double heightConstraint) [0x00053] in D:\a\s\Xamarin.Forms.Core\VisualElement.cs:644
11-06 14:17:23.939 I/MonoDroid( 5445): at Xamarin.Forms.Layout.GetSizeRequest (System.Double widthConstraint, System.Double heightConstraint) [0x00000] in D:\a\s\Xamarin.Forms.Core\Layout.cs:132
11-06 14:17:23.939 I/MonoDroid( 5445): at Xamarin.Forms.VisualElement.Measure (System.Double widthConstraint, System.Double heightConstraint, Xamarin.Forms.MeasureFlags flags) [0x00054] in D:\a\s\Xamarin.Forms.Core\VisualElement.cs:702
11-06 14:17:23.939 I/MonoDroid( 5445): at Xamarin.Forms.StackLayout.CalculateNaiveLayout (Xamarin.Forms.StackLayout+LayoutInformation layout, Xamarin.Forms.StackOrientation orientation, System.Double x, System.Double y, System.Double widthConstraint, System.Double heightConstraint) [0x000a8] in D:\a\s\Xamarin.Forms.Core\StackLayout.cs:163
11-06 14:17:23.939 I/MonoDroid( 5445): at Xamarin.Forms.StackLayout.CalculateLayout (Xamarin.Forms.StackLayout+LayoutInformation layout, System.Double x, System.Double y, System.Double widthConstraint, System.Double heightConstraint, System.Boolean processExpanders) [0x00058] in D:\a\s\Xamarin.Forms.Core\StackLayout.cs:123
11-06 14:17:23.939 I/MonoDroid( 5445): at Xamarin.Forms.StackLayout.LayoutChildren (System.Double x, System.Double y, System.Double width, System.Double height) [0x0004e] in D:\a\s\Xamarin.Forms.Core\StackLayout.cs:58
11-06 14:17:23.939 I/MonoDroid( 5445): at Xamarin.Forms.Layout.UpdateChildrenLayout () [0x00158] in D:\a\s\Xamarin.Forms.Core\Layout.cs:266
11-06 14:17:23.939 I/MonoDroid( 5445): at Xamarin.Forms.Layout.OnSizeAllocated (System.Double width, System.Double height) [0x0000f] in D:\a\s\Xamarin.Forms.Core\Layout.cs:224
11-06 14:17:23.939 I/MonoDroid( 5445): at Xamarin.Forms.VisualElement.SizeAllocated (System.Double width, System.Double height) [0x00000] in D:\a\s\Xamarin.Forms.Core\VisualElement.cs:784
11-06 14:17:23.939 I/MonoDroid( 5445): at Xamarin.Forms.Layout.ForceLayout () [0x00000] in D:\a\s\Xamarin.Forms.Core\Layout.cs:125
11-06 14:17:23.939 I/MonoDroid( 5445): at Xamarin.Forms.Platform.Android.ViewCellRenderer+ViewCellContainer.Update (Xamarin.Forms.ViewCell cell) [0x00185] in D:\a\s\Xamarin.Forms.Platform.Android\Cells\ViewCellRenderer.cs:178
11-06 14:17:23.939 I/MonoDroid( 5445): at Xamarin.Forms.Platform.Android.ViewCellRenderer.GetCellCore (Xamarin.Forms.Cell item, Android.Views.View convertView, Android.Views.ViewGroup parent, Android.Content.Context context) [0x00027] in D:\a\s\Xamarin.Forms.Platform.Android\Cells\ViewCellRenderer.cs:21
11-06 14:17:23.939 I/MonoDroid( 5445): at Xamarin.Forms.Platform.Android.CellRenderer.GetCell (Xamarin.Forms.Cell item, Android.Views.View convertView, Android.Views.ViewGroup parent, Android.Content.Context context) [0x00075] in D:\a\s\Xamarin.Forms.Platform.Android\Cells\CellRenderer.cs:51
11-06 14:17:23.939 I/MonoDroid( 5445): at Xamarin.Forms.Platform.Android.CellFactory.GetCell (Xamarin.Forms.Cell item, Android.Views.View convertView, Android.Views.ViewGroup parent, Android.Content.Context context, Xamarin.Forms.View view) [0x0001e] in D:\a\s\Xamarin.Forms.Platform.Android\Cells\CellFactory.cs:20
11-06 14:17:23.939 I/MonoDroid( 5445): at Xamarin.Forms.Platform.Android.ListViewAdapter.GetView (System.Int32 position, Android.Views.View convertView, Android.Views.ViewGroup parent) [0x00200] in D:\a\s\Xamarin.Forms.Platform.Android\Renderers\ListViewAdapter.cs:299
11-06 14:17:23.939 I/MonoDroid( 5445): at Android.Widget.BaseAdapter.n_GetView_ILandroid_view_View_Landroid_view_ViewGroup_ (System.IntPtr jnienv, System.IntPtr native__this, System.Int32 position, System.IntPtr native_convertView, System.IntPtr native_parent) [0x0001a] in <11a340ccc8de43f09c97400139266ef5>:0
11-06 14:17:23.939 I/MonoDroid( 5445): at (wrapper dynamic-method) Android.Runtime.DynamicMethodNameCounter.68(intptr,intptr,int,intptr,intptr)
11-06 14:17:23.950 W/.TheStylingRoo( 5445): JNI RegisterNativeMethods: attempt to register 0 native methods for android.runtime.JavaProxyThrowable
11-06 14:17:23.954 D/Mono ( 5445): DllImport searching in: '__Internal' ('(null)').
11-06 14:17:23.955 D/Mono ( 5445): Searching for 'java_interop_jnienv_throw'.
11-06 14:17:23.955 D/Mono ( 5445): Probing 'java_interop_jnienv_throw'.
11-06 14:17:23.955 D/Mono ( 5445): Found as 'java_interop_jnienv_throw'.
**System.NullReferenceException:** 'Object reference not set to an instance of an object.'
这个让我难住了。当尝试加载一个显示客户列表的页面和另一个加载预订的页面时,会触发异常。请注意,启动页面具有相同的逻辑来加载当前预订列表,并且工作正常。
我搜索了所有相关问题并尝试了以下解决方案:
- 单步执行代码。它在 2 个页面构造函数中的任何一个结束时触发,不包括启动页面构造函数。这些构造函数中的所有方法都可以正常执行。
- 正在寻找空值。没有对象或属性为空。
- 正在发表评论,尝试捕捉和 if !'s。 None 这三个防止异常。
- 正在重新创建 xaml 事件。我确定是这个,但不是。
- 正在查看异常详细信息。异常弹出窗口根本没有详细信息。
- 更改异步方法。显示预订的启动视图实际上是一个副本,包括异步方法。它工作正常。
- 正在删除绑定。
- Android 模拟器的清理、重建、出厂重置。各种顺序
- 正在重启Visual Studio
- 正在删除 bin 文件夹并重新加载
相关代码:
MainPage.xaml.cs(只是加载 ClientsPage 的事件)
private async void Button_Clicked(object sender, EventArgs e)
{
try
{
await Navigation.PushAsync(new Clients_Page());
}
catch(Exception ex)
{
Console.WriteLine(ex);
}
}
Clients_Page.xaml.cs(只是构造函数)
public partial class Clients_Page : ContentPage
{
public Clients_Page()
{
InitializeComponent();
Clients_ViewModel viewModel = new Clients_ViewModel();
viewModel.LoadClients();
BindingContext = viewModel;
// Commenting all but InitializeComponent (Including Bindings in xaml) has no effect, so the ViewModel is not to blame
} // EXCEPTION FIRES AFTER THIS LINE
Clients_ViewModel.cs
class Clients_ViewModel : INotifyPropertyChanged
{
//PROPERTIES_________________________________________________________
public event PropertyChangedEventHandler PropertyChanged;
private IRepository database { get; }
private ObservableCollection<Client> clients;
public ObservableCollection<Client> Clients
{
get { return clients; }
set
{
if (value != clients)
{
clients = value;
if (PropertyChanged != null)
{
NotifyPropertyChanged("Clients");
}
}
}
}
public async void LoadClients()
{
Clients = new ObservableCollection<Client>(await database.GetAllClients());
}
private void NotifyPropertyChanged(String propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (null != handler)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
public Clients_ViewModel()
{
database = App.Database_IRepository;
}
}//endclass
}//endnamespace
Repository.cs(仅 GetAllClients 任务)
public async Task<List<Client>> GetAllClients()
{
return await database.Table<Client>().OrderBy(c => c.FirstName).ToListAsync();
}
Xaml 只是一个使用 ItemsSource="{Binding Clients}" 的 ListView 和一个使用 Text="{Binding FirstName}" 的 ViewCell"
我精通调试,并且会很高兴找到具有空对象引用的方法。我只需要知道如何让调试器显示所述方法。
更新 1:即使删除了 xaml 中的所有绑定并且删除了对视图模型的调用,也会触发异常。我也试过评论整个 Clients_Page.xaml 除了 ContentPage 和主网格,错误仍然存在。所以它不能是 ViewModel 或 xaml
更新 2:即使更改点击事件以导航到新页面也会出错
已解决
我不得不删除 NavigationPage.SetHasNavigationBar(this, false);来自 Main_Page.xaml.cs 构造函数(完整块如下所示)
public Main_Page()
{
InitializeComponent();
//NavigationPage.SetHasNavigationBar(this, false);
Main_ViewModel viewModel = new Main_ViewModel();
viewModel.LoadCurrentBookings();
BindingContext = viewModel;
}
我添加该行是为了在我的应用 运行 时删除难看的 Android 导航栏,但是 Android 页面导航似乎以某种方式链接到该栏,所以我的启动视图无法导航到任何其他设置为 false
的视图