C# WPF在XAML上获取类实例空引用
我在尝试为我的XAML设置类实例时得到空引用 XAML代码C# WPF在XAML上获取类实例空引用,c#,wpf,xaml,C#,Wpf,Xaml,我在尝试为我的XAML设置类实例时得到空引用 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:
<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.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);
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Navigation;
使用System.Windows.Shapes;
使用WpfApplication1.class;
命名空间WpfApplication1
{
///
///MainWindow.xaml的交互逻辑
///
公共部分类主窗口:窗口
{
个人;
公共主窗口()
{
初始化组件();
person=新的person();
}
私有无效btnSayHello_单击(对象发送者,路由目标e)
{
string message=string.Format(“你好{0}{1},你告诉我你{2}岁。”,person.Name,person.LastName,person.Age);
MessageBox.Show(message);
}
}
}
我不知道为什么会得到这个空引用,因为类是在MainWindow()方法上创建的
有人有线索吗
谢谢大家。如果您查看异常堆栈跟踪,就会看到,
PropertyChanged
为空。检查是否(PropertyName!=null)
并不能阻止这种情况。读取堆栈跟踪。天哪,我不敢相信我没有看到。非常感谢。
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);
}
}
}