Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为简单人员类实现iNotifyPropertyChanged会导致VisualStudio XAML设计器崩溃_C#_Visual Studio 2010_Xaml_Inotifypropertychanged - Fatal编程技术网

C# 为简单人员类实现iNotifyPropertyChanged会导致VisualStudio XAML设计器崩溃

C# 为简单人员类实现iNotifyPropertyChanged会导致VisualStudio XAML设计器崩溃,c#,visual-studio-2010,xaml,inotifypropertychanged,C#,Visual Studio 2010,Xaml,Inotifypropertychanged,我有个奇怪的问题。我所拥有的只是XAML中的一个文本框,绑定到Person类。当我在Person类中实现iNotifyPropertyChanged时,Visual Studio XAML设计器崩溃,如果我只是运行项目,我会得到堆栈溢出异常 当我删除iNotifyPropertyChanged时,一切正常,textbox绑定到Person类中的FirstName字段 这是我的XAML,没什么特别的,只是一个数据绑定的文本框 <Window x:Class="DataBinding_With

我有个奇怪的问题。我所拥有的只是XAML中的一个文本框,绑定到
Person
类。当我在
Person
类中实现
iNotifyPropertyChanged
时,Visual Studio XAML设计器崩溃,如果我只是运行项目,我会得到堆栈溢出异常

当我删除iNotifyPropertyChanged时,一切正常,textbox绑定到Person类中的FirstName字段

这是我的XAML,没什么特别的,只是一个数据绑定的文本框

<Window x:Class="DataBinding_WithClass.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525"
        xmlns:c="clr-namespace:DataBinding_WithClass">
    <Grid x:Name="myGrid" >
        <Grid.Resources>
            <c:Person x:Key="MyPerson" />            
        </Grid.Resources>
        <Grid.DataContext>
            <Binding Source="{StaticResource MyPerson}"/>
        </Grid.DataContext>
        <TextBox Text="{Binding FirstName}" Width="150px"/>

    </Grid>
</Window>
我试过了

重新启动Visual Studio 2012(在windows 7 Home Premium 64位上运行)

启动一个新的空白项目-同样的问题

奇怪的是,如果没有iNotifyPropertyChanged,一切都很好,但是我的文本框将不会得到更新,因为我的*Person*类中的名字发生了变化


您遇到过这个问题吗?

您不正确地实现了该类。您需要一个支持字段:

private string firstName;
public string FirstName
{
     get { return this.firstName; }
     set
     {
         if(this.firstName != value)
         {
            this.firstName = value; // Set field
            OnPropertyChanged("FirstName");
         }
     }
}
现在,getter正在获取自身,setter设置属性本身,这两个属性都将导致
StackOverflowException

private string firstName;
public string FirstName
{
     get { return this.firstName; }
     set
     {
         if(this.firstName != value)
         {
            this.firstName = value; // Set field
            OnPropertyChanged("FirstName");
         }
     }
}