线程中的ApplicationException未触发WPF C#app中的验证样式
我正在开发一个应用程序,允许用户输入需要符合特定标准的ID值。在视图模型中,我有一个属性来保存该值,该属性执行一些初始验证(工作正常),然后启动一个线程,自动在另一个系统中查找输入的值。最后一部分比较慢,所以我把它放在了另一个线程中。问题是当线程中的ApplicationException未触发WPF C#app中的验证样式,c#,wpf,multithreading,validation,exception-handling,C#,Wpf,Multithreading,Validation,Exception Handling,我正在开发一个应用程序,允许用户输入需要符合特定标准的ID值。在视图模型中,我有一个属性来保存该值,该属性执行一些初始验证(工作正常),然后启动一个线程,自动在另一个系统中查找输入的值。最后一部分比较慢,所以我把它放在了另一个线程中。问题是当ApplicationException被抛出该线程时,它不会触发表单中文本框的验证样式。以下是我的财产的外观: private string idNumber; public string IdNumber { ge
ApplicationException
被抛出该线程时,它不会触发表单中文本框的验证样式。以下是我的财产的外观:
private string idNumber;
public string IdNumber
{
get { return idNumber; }
set
{
idNumber = value;
OnPropertyChanged("IdNumber");
if (String.IsNullOrEmpty(idNumber))
{
throw new ApplicationException("The ID Number is required.");
}
if (idNumber.Length < 8)
{
throw new ApplicationException("The ID Number should be 8 alphanumeric characters.");
}
Thread Validate = new Thread(ValidateIdNumber);
Validate.Start();
}
}
那么,是否有可能将这个ApplicationException
路由到该文本框,从而触发验证样式
同样,最初的验证可以工作,但后来的验证不能。此外,我需要将集合检查向上移动到集合中的初始验证。在UI中绑定一个属性,并使用异常消息更新该属性。不幸的是,问题在于您使用的异常错误。异常不应用于触发逻辑。您需要找到一种不使用异常来传递验证数据的方法。您现在所做的是在线程中抛出一个异常,这会导致线程中止,因为您没有在任何地方捕获它(我可以看到)。
private void ValidateSecurity()
{
if (this.BadIdNumbers.Contains(this.IdNumber))
{
throw new ApplicationException("The ID Number you have entered is on either the Bad ID Number list.");
}
OurAutomationLibrary AutomatedApp = new OurAutomationLibrary(this.user, this.pass);
AutomatedApp.Get(this.IdNumber);
if (AutomatedApp.GetFile().Contains("ID-NOT-ON-FILE"))
{
AutomatedApp.Quit();
throw new ApplicationException("The ID Number you have entered is not on file.");
}
if (AutomatedApp.Read(2, 70, 5).Trim() == "")
{
AutomatedApp.Quit();
throw new ApplicationException("This ID Number is below $1.00.");
}
AutomatedApp.Quit();
}
}