App.xaml MVVM指示灯中的空参考错误 [英] Null reference error in App.xaml MVVM light
问题描述
我将在一个窗口中创建一个WPF应用程序,并通过更改Frame
的内容来导航我的应用程序.为此,我使用了MVVM灯.
I'll make a WPF application whit one window and by changing the content of the Frame
I'll navigate troth my application. For this I'm using MVVM light.
但是在App.xaml
上,我在Visual Studio的错误列表中遇到了此错误.
But on App.xaml
I've got this error in the error list of Visual Studio.
对象引用未设置为对象的实例.
Object reference not set to an instance of an object.
以下是发生错误的代码:
Here is the code where the error happens:
<Application
x:Class="Project.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Project"
StartupUri="MainWindow.xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
d1p1:Ignorable="d"
xmlns:vm="clr-namespace:Project.ViewModel"
xmlns:services="clr-namespace:Project.Services"
xmlns:d1p1="http://schemas.openxmlformats.org/markup-compatibility/2006">
<Application.Resources>
<ResourceDictionary>
<services:IocContainer x:Key="ioc" />
<vm:ApplicationViewModel x:Key="appvm" d:IsDataSource="True" /> <!-- error happens on this line -->
</ResourceDictionary>
</Application.Resources>
</Application>
这是我的MainWindow
:
<Window x:Class="Project.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Project"
mc:Ignorable="d"
DataContext="{StaticResource appvm}"
Title="Project" Height="450" Width="800">
<Frame>
<Frame.Content>
<Page Content="{Binding CurrentPage, Mode=TwoWay}" />
</Frame.Content>
</Frame>
</Window>
这是我的ApplicationViewModel
,它是从ViewModelBase
继承的:
Here is my ApplicationViewModel
that inherits from ViewModelBase
:
public class ApplicationViewModel : ViewModelBase
{
private Page _currentPage = IocContainer.Ioc.StartScreenPage;
public Page CurrentPage
{
get
{
return _currentPage;
}
set
{
if (_currentPage != value)
{
_currentPage = value;
OnPropertyChanged();
}
}
}
public StartScreenViewModel StartScreenViewModel
{
get
{
return (App.Current.Resources["ioc"] as IocContainer)?.StartScreenViewModel;
}
}
public void Navigate(Type sourcePageType)
{
}
}
这是实现INotifyPropertyChanged
的ViewModelBase
.
public class ViewModelBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
Debug.WriteLine("PropertyChanged is niet null ☺");
}
else
{
Debug.WriteLine("PropertyChanged is null");
}
}
}
这是我的IoC容器:
public class IocContainer
{
static IocContainer()
{
SimpleIoc.Default.Register<ApplicationViewModel>(false);
SimpleIoc.Default.Register<StartScreenViewModel>(false);
SimpleIoc.Default.Register<StartScreenPage>(false);
}
public static IocContainer Ioc
{
get { return App.Current.Resources["ioc"] as IocContainer; }
}
public ApplicationViewModel ApplicationViewModel
{
get { return SimpleIoc.Default.GetInstance<ApplicationViewModel>(); }
}
public StartScreenPage StartScreenPage
{
get { return SimpleIoc.Default.GetInstance<StartScreenPage>(); }
}
public StartScreenViewModel StartScreenViewModel
{
get { return SimpleIoc.Default.GetInstance<StartScreenViewModel>(); }
}
}
这是我的StartScreenPage
:
<Page x:Class="Project.StartScreenPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:Project"
mc:Ignorable="d"
DataContext="{Binding StartScreenViewModel, Source={StaticResource ioc}}"
Title="StartScreen">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto" />
</Grid.ColumnDefinitions>
<Label Content="Hello world" Grid.Row="0" Grid.Column="0" />
</Grid>
</Page>
这是StartScreenViewModel
.
public class StartScreenViewModel : ViewModelBase
{ }
所有应用程序,窗口和页面都有默认的构造函数,该构造函数调用InitializeComponent
.
All the application, window and pages have a default constructor that calls InitializeComponent
.
我可以运行我的应用程序,但是我看到一个空窗口.
I can run my application but I see an empty window.
我忘了什么吗?
Continuing on my anwser on this question: Page
can have only Frame
as parent and not Window
, I've changed my code of the MainWindow
to this:
MainWindow
上的代码必须是这样的:
The code on the
MainWindow
must be this:
<!-- Opening Window tag with all attributes -->
<Frame Content="{Binding CurrentPage, Mode=TwoWay}" />
<!-- Closing Window tag -->
这还将在窗口中显示StartScreenPage
.
但是仍然会引发空引用错误.
However the null reference error is still being thrown.
推荐答案
在代码中几乎没有null检查,这就是这种情况.
There's very little null checking in your code, which is where this is happening.
找到问题的最佳方法是转到Visual Studio工具面板
The best way to find the issue is to go to the Visual Studio tool panel
调试→ Windows→例外设置
并完全检查标有'Common Language Runtime Exceptions
'的行.再次运行代码时,您应该获得有关null异常发生位置的更多信息.
and fully check the row labelled 'Common Language Runtime Exceptions
'. When you run the code again, you should get more information about where the null exception is happening.
这篇关于App.xaml MVVM指示灯中的空参考错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!