在WPF 應用程序下出現:Invisible or disabled control cannot be activated(不見的或禁用的控件不能被激活)錯誤。線程
System.ArgumentException: Invisible or disabled control cannot be activated
at System.Windows.Forms.ContainerControl.SetActiveControlInternal(Control value)
at System.Windows.Forms.ContainerControl.SetActiveControl(Control ctl)
at System.Windows.Forms.ContainerControl.set_ActiveControl(Control value)
at System.Windows.Forms.Integration.WindowsFormsHost.RestoreFocusedChild()
at System.Windows.Forms.Control.InvokeMarshaledCallbackDo(ThreadMethodEntry tme)
at System.Windows.Forms.Control.InvokeMarshaledCallbackHelper(Object obj)
at System.Threading.ExecutionContext.runTryCode(Object userData)
at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Windows.Forms.Control.InvokeMarshaledCallback(ThreadMethodEntry tme)
at System.Windows.Forms.Control.InvokeMarshaledCallbacks()code
解決辦法:orm
該錯誤是線程的問題,對程序運行無影響,能夠考慮過濾該錯誤。get
方法以下:string
1 確保刪除的控件不是不見的或禁用的控件。it
2 過濾該錯誤方法。io
public MainWindow()
{
System.Windows.Forms.Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
}
void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
{
if (!FailureFromFocusedChild(e.Exception))
{
System.Windows.Forms.Application.ThreadExceptionDialog dlg;
dlg = new System.Windows.Forms.Application.ThreadExceptionDialog(e.Exception);
dlg.ShowDialog();
}
private bool FailureFromFocusedChild(Exception e)
{
bool result = false;
string stackTrace = e.StackTrace;
result = (e is System.ArgumentException) && (e.Source == "System.Windows.Forms")
&& (stackTrace.IndexOf("System.Windows.Forms.Integration.WindowsFormsHost.RestoreFocusedChild")>=0);
return result;
}sed