gpt4 book ai didi

c# - App.xaml MVVM指示灯中的空引用错误

转载 作者:行者123 更新时间:2023-12-03 10:32:14 25 4
gpt4 key购买 nike

我将在一个窗口中创建一个WPF应用程序,并通过更改Frame的内容来导航我的应用程序。为此,我正在使用MVVM灯。

但是在App.xaml上,我在Visual Studio的错误列表中遇到了此错误。

Object reference not set to an instance of an object.



这是发生错误的代码:

<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:

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)
{
}
}

这是实现 ViewModelBaseINotifyPropertyChanged

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

我可以运行我的应用程序,但看到一个空窗口。

我忘了什么吗

编辑:继续回答这个问题: Page can have only Frame as parent and not Window ,我将 MainWindow的代码更改为:

The code on the MainWindow must be this:

<!-- Opening Window tag with all attributes -->
<Frame Content="{Binding CurrentPage, Mode=TwoWay}" />
<!-- Closing Window tag -->

This will also show the StartScreenPage on the window.



但是,仍然会引发空引用错误。

最佳答案

您的代码中几乎没有null检查,这就是这种情况的发生。

找到问题的最佳方法是转到Visual Studio工具面板

调试→Windows→异常(exception)设置

并完全检查标有“Common Language Runtime Exceptions”的行。当您再次运行代码时,您应该获得有关在哪里发生null异常的更多信息。

关于c# - App.xaml MVVM指示灯中的空引用错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54008872/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com