C# 异常不显示
我有:C# 异常不显示,c#,wpf,xaml,exception,C#,Wpf,Xaml,Exception,我有: public class Person : INotifyPropertyChanged { private string _name; public int Age { get; set; } public string Name { get { return _name; } set { if (!Stri
public class Person : INotifyPropertyChanged
{
private string _name;
public int Age { get; set; }
public string Name
{
get { return _name; }
set
{
if (!String.IsNullOrEmpty(_name))
{
if (String.IsNullOrEmpty(value))
{
throw new Exception("name couldn't be null");
}
else if ((_name.Equals(value) != true))
{
if (!String.IsNullOrEmpty(value))
{
throw new Exception("name couldn't be null");
}
else
{
InvokePropertyChanged("_name");
}
_name = value;
}
}
else if (String.IsNullOrEmpty(value))
{
throw new Exception("name couldn't be null");
}
else
{
_name = value;
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void InvokePropertyChanged(string propertyName)
{
var e = new PropertyChangedEventArgs(propertyName);
PropertyChangedEventHandler changed = PropertyChanged;
if (changed != null) changed(this, e);
}
为什么我调试并进入以下行:
抛出新异常(“名称不能为null”)
异常未显示。从我看到的情况来看,您没有将控件设置为绑定到person对象。因此,它不应该试图设置它并随后出错
当您在getter中放置断点时,它是否会被调用?从我看到的情况来看,您没有将控件绑定到person对象。因此,它不应该试图设置它并随后出错
当您在getter中放置断点时,是否会调用它?我认为您希望将Person对象分配给控件的DataContext:
public UserControl1()
{
InitializeComponent();
Person person = new Person();
person.Name = "Patrick";
this.DataContext = person;
}
您可能还希望对绑定进行设置,以便在setter中引发异常时UI将显示错误模板
<TextBox Name="tbName" Text="{Binding Path=Name, Mode=TwoWay, ValidatesOnExceptions=True}"></TextBox>
我想您应该将Person对象分配给控件的DataContext:
public UserControl1()
{
InitializeComponent();
Person person = new Person();
person.Name = "Patrick";
this.DataContext = person;
}
您可能还希望对绑定进行设置,以便在setter中引发异常时UI将显示错误模板
<TextBox Name="tbName" Text="{Binding Path=Name, Mode=TwoWay, ValidatesOnExceptions=True}"></TextBox>
请参见此图,但基本上异常将由绑定处理和抑制。请参见此图,但基本上异常将由绑定处理和抑制。尝试执行以下操作:
...
else if (String.IsNullOrEmpty(value))
{
try{
throw new Exception("name couldn't be null");
}
catch(Exception ex)
{
//Set Breakpoint Below
int x=0;
}
}
...
您将看到您的代码将出现在catch块中,并且会抛出异常。但是,异常的处理取决于您。最好的方法已经由Quartermeister提出
否则,根据您的VS IDE设置,异常将被抑制,并且您不会在屏幕上看到错误。尝试执行以下操作:
...
else if (String.IsNullOrEmpty(value))
{
try{
throw new Exception("name couldn't be null");
}
catch(Exception ex)
{
//Set Breakpoint Below
int x=0;
}
}
...
您将看到您的代码将出现在catch块中,并且会抛出异常。但是,异常的处理取决于您。最好的方法已经由Quartermeister提出
否则,根据您的VS IDE设置,异常将被抑制,并且您不会在屏幕上看到错误。是否确实更新了调试数据库(.pdb)?您可能需要进行完整的重建。另外,当你说没有显示异常时,你是什么意思?它没有显示在哪里?它的意思是:我在抛出新异常(“名称不能为空”)的第行,我按F10,什么也没有发生。应用程序仍在工作。是否确实已更新调试数据库(.pdb)?您可能需要进行完整的重建。另外,当你说没有显示异常时,你是什么意思?它没有显示在哪里?它的意思是:我在抛出新异常(“名称不能为空”)的第行,我按F10,什么也没有发生。应用程序仍在工作。对不起,我忘记了第this.DataContext=person;行的内容;。所以,是的,我进入了getter。对不起,我忘记了过去的第行this.DataContext=person;。所以,是的,我进入了getter。这意味着异常被正确抛出。现在你到底想用它做什么?不可能。我运行.exe,当我在文件中输入错误的数据时,我不会出现异常。所以这不取决于我在VisualStudio中的设置。通常我会得到关于异常的信息。我错过了:/n异常在绑定级别被禁止。您可以使用Catch(Exception ex){Messagebox.show(“Exception Desc:+ex.ToString());}在Catch块中手动显示信息。这意味着异常被正确抛出。现在你到底想用它做什么?不可能。我运行.exe,当我在文件中输入错误的数据时,我不会出现异常。所以这不取决于我在VisualStudio中的设置。通常我会得到关于异常的信息。我错过了:/n异常在绑定级别被禁止。您可以使用Catch(Exception ex){Messagebox.show(“Exception Desc:+ex.ToString());}在Catch块中手动显示信息@phenevo:如果希望Validation.ErrorTemplate显示红色边框以外的内容,可以将其设置为其他控件模板。@phenevo:如果希望Validation.ErrorTemplate显示红色边框以外的内容,可以将其设置为其他控件模板。