C# 目标元素对应于不再可用的UI

C# 目标元素对应于不再可用的UI,c#,.net,ui-automation,microsoft-ui-automation,C#,.net,Ui Automation,Microsoft Ui Automation,此代码在最后一行引发异常 目标元素对应于不再可用的UI(例如,父窗口已关闭)。 在MS.Internal.AutomationProxies.Misc.Throwwin32异常传送错误(Int32错误代码) 在MS.Internal.AutomationProxies.Misc.ProxySendMessage(IntPtr hwnd、Int32 msg、IntPtr wParam、IntPtr lParam)处 在MS.Internal.AutomationProxies.WindowsLis

此代码在最后一行引发异常

目标元素对应于不再可用的UI(例如,父窗口已关闭)。
在MS.Internal.AutomationProxies.Misc.Throwwin32异常传送错误(Int32错误代码)
在MS.Internal.AutomationProxies.Misc.ProxySendMessage(IntPtr hwnd、Int32 msg、IntPtr wParam、IntPtr lParam)处
在MS.Internal.AutomationProxies.WindowsListBox.ListboxItem.System.Windows.Automation.Provider.ISelectionItemProvider.get_IsSelected()中

那么,这里的问题是什么


另外,另一个问题是,
SelectionItemPattern
中的“当前”是什么意思?(我确信这并不意味着当前选定的元素。)

我也面临同样的问题。我在一行中获取automationElement,并通过第二行中的
Current
属性访问它`当您访问时,Current属性再次转到目标应用程序的进程以获取值。因此,在这个跨进程调用期间,如果您的ui元素可能不再可用,则会发生此异常。即使您有automationElement对象,它也将无效


如果您只想获取值,可以尝试使用
CacheRequet
,然后使用automationELement对象的
Cached
属性。

如果您使用WPR并且组合尚未初始化,通常从数据库中读取需要很长时间的项,并访问组合,则会出现此异常。 因此,您需要等待组合在循环中初始化(可能是睡眠)

AE_ComboBox = uia.GetElementByControlTypeAndAutomationId(globalSettings, ControlType.ComboBox, "1");
AE_TargetValue = uia.GetElement(AE_ComboBox, value, true);
SelectionItemPattern SIP = AE_TargetValue.GetCurrentPattern(SelectionItemPattern.Pattern) as SelectionItemPattern;
SIP.Select();
Thread.Sleep(2000);
SIP.Current.IsSelected