WPF XAML对象引用未设置为对象的实例

本文关键字:对象 实例 设置 XAML 对象引用 WPF | 更新日期: 2024-10-18 21:49:10

我在开发过程中遇到过这个问题,编译器给出的问题是没有设置对象引用,所以它应该是某种空指针

 <Window x:Class="BattleShip.MVVM.Views.NewGameSettings"
            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:viewModels="clr-namespace:BattleShip.MVVM.ViewModels"
            mc:Ignorable="d"
            Title="New Game" ResizeMode="NoResize" SizeToContent="WidthAndHeight" WindowStartupLocation="CenterScreen">
        <Window.DataContext>
            <viewModels:SettingsViewModel/>
        </Window.DataContext><Grid>
    <Grid.RowDefinitions>
        <RowDefinition MinHeight="80" Height="Auto"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition MinHeight="30" Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition MinWidth="80" Width="Auto"/>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition MinWidth="80" Width="Auto"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <Image Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="4" Source="{StaticResource Background}" Height="120" Stretch="Fill"/>
    <Label
        x:Name="HeightLabel"
        Content="Height"
        Grid.Column="1" Grid.Row="1"
        Margin="0,10,0,0"/>
    <Label
        x:Name="WidthLabel"
        Content="Width"
        Grid.Column="1" Grid.Row="2"
        Margin="0,10,0,0"/>
    <Label
        x:Name="DifficultyLabel"
        Content="AI Difficulty"
        Grid.Column="1" Grid.Row="3"
        Margin="0,10,0,0"/>
    <TextBox x:Name="LengthBox" 
             TextWrapping="NoWrap"
             VerticalContentAlignment="Center"
             IsInactiveSelectionHighlightEnabled="True" 
             Grid.Column="2" 
             Grid.Row="1" 
             HorizontalAlignment="Left" 
             Width="100"
             Text="{Binding Height}"
             Margin="0,10,0,0"/>
    <TextBox x:Name="HeightBox" 
             TextWrapping="NoWrap" 
             VerticalContentAlignment="Center"
             Text="{Binding Width}" 
             Grid.Column="2" Grid.Row="2" 
             HorizontalAlignment="Left" 
             Width="100"
             Margin="0,10,0,0"/>
    <ComboBox ItemsSource="{Binding Difficulties}"
              SelectedItem="{Binding SelectedDifficulty}"
              Grid.Column="2" Grid.Row="3" 
              VerticalContentAlignment="Center"
              HorizontalAlignment="Left" 
              Width="100"
              Margin="0,10,0,0"/>
    <Button Grid.Column="1" Grid.Row="4" Grid.ColumnSpan="2" Content="Start" Margin="0,10,0,0" Command="{Binding StartGameCommand}"/>
</Grid>

这是它给出错误的代码(第行,共行)。自从初始化此视图模型以来,如何解决此问题?

WPF XAML对象引用未设置为对象的实例

如果您有正确的ViewModel,编译器错误可能是由Visual Studio的错误引起的,该错误发生在VS 2015、VS2013等中。因此解决方案是:

请尝试删除.soo文件,然后重新启动Visual Studio。