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

问题描述 投票:0回答:2
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

这段代码在最后一行抛出异常。

目标元素对应不再可用的 UI(例如,父窗口已关闭)。
在 MS.Internal.AutomationProxies.Misc.ThrowWin32ExceptionsIfError(Int32 errorCode)
在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
中的“当前”是什么意思? (我确信这并不意味着当前选定的元素。)

c# .net ui-automation microsoft-ui-automation
2个回答
1
投票

我也面临同样的问题。我在一行中获取 AutomationElement,并通过第二行中的

Current
属性访问它。当您访问
Current
属性时,它会再次转到目标应用程序的进程中获取值。因此,在这个跨进程调用期间,如果您的 UI 元素可能不再可用,则会发生此异常。即使你有一个automationElement对象,它也是无效的。

如果您只想获取值,您可以尝试使用

CacheRequet
,然后使用automationELement对象的
Cached
属性。


0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.