WPF在XAML上获取类实例空引用

时间:2015-11-22 14:50:15

标签: c# wpf xaml

我在尝试为我的XAML设置类实例时获得了null引用。

XAML代码

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication1"
        xmlns:classes="clr-namespace:WpfApplication1.Classes"
        Title="MainWindow" Height="350" Width="525">

    <Window.Resources>
        <classes:Person x:Key="person" /> <!-- here getting null reference -->
        <Style TargetType="TextBlock">
            <Setter Property="Foreground" Value="Blue" />
            <Setter Property="FontSize" Value="30" />
            <Setter Property="FontWeight" Value="Bold" />
            <Setter Property="FontFamily" Value="Arial" />
        </Style>
        <Style TargetType="TextBox">
            <Setter Property="Foreground" Value="Black" />
            <Setter Property="Background" Value="LightGray" />
        </Style>
        <Style TargetType="Button">
            <Setter Property="Foreground" Value="DodgerBlue" />
            <Setter Property="BorderThickness" Value="0" />
            <Setter Property="Background" Value="White" />
        </Style>
    </Window.Resources>

    <StackPanel x:Name="stackPanelPerson" DataContext="{Binding Source={ StaticResource person }}">
        <TextBlock Text="Write your name" />
        <TextBox x:Name="boxUserName" Text="{Binding Name, Mode=TwoWay}"
                 Margin="0 20 0 0" />
        <TextBox x:Name="boxLastName" Text="{Binding LastName, Mode=TwoWay}"
                 Margin="0 20 0 0" />
        <TextBox x:Name="boxAge" Text="{Binding Age, Mode=TwoWay}"
                 Margin="0 20 0 0" />
        <Button x:Name="btnSayHello"
                Click="btnSayHello_Click"
                Margin="0 20 0 0">Say Hello</Button>
    </StackPanel>
</Window>

这是我的班级代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WpfApplication1.Classes
{
    public class Person : INotifyPropertyChanged
    {
        public Person()
        {
            this.Name = "Name";
            this.LastName = "LastName";
            this.Age = 22;
        }

        private String name;

        public String Name
        {
            get { return name; }
            set 
            { 
                name = value;
                OnPropertyChanged("Name");
                OnPropertyChanged("FullName");
            }
        }

        private String lastName;

        public String LastName
        {
            get { return lastName; }
            set 
            { 
                lastName = value;
                OnPropertyChanged("LastName");
                OnPropertyChanged("FullName");
            }
        }

        private int age;

        public int Age
        {
            get { return age; }
            set 
            { 
                age = value;
                OnPropertyChanged("Age");
            }
        }

        private String fullName;

        public String FullName
        {
            get { return string.Format("{0} {1}", Name, LastName); }
            set 
            { 
                fullName = value;
                OnPropertyChanged("FullName");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(string PropertyName)
        {
            if (PropertyName != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
            }
        }
    }
}

这里是主窗口代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WpfApplication1.Classes;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        Person person;

        public MainWindow()
        {
            InitializeComponent();
            person = new Person();
        }

        private void btnSayHello_Click(object sender, RoutedEventArgs e)
        {
            string message = string.Format(" Hello {0} {1}, you told me that you are {2} years old. ", person.Name, person.LastName, person.Age);

            MessageBox.Show(message);
        }
    }
}

我不知道为什么我得到这个空引用,因为该类是在MainWindow()方法上创建的。

任何人都有线索?

谢谢你们。

1 个答案:

答案 0 :(得分:3)

当您查看是否查看异常堆栈跟踪时,PropertyChanged为空。检查if (PropertyName != null)并不能阻止这种情况。