我正在动态创建一个TextBox Control
,并且已经订阅了TextBox LosingFocus Event
,它在本地计算机上工作正常,但是在虚拟机上却出现了异常。
出现以下错误:
无法将类型为“ Windows.UI.Xaml.Controls.TextBox”的对象转换为 键入“ Windows.UI.Xaml.IUIElement5
和Stacktrace是:
at System.StubHelpers.StubHelpers.GetCOMIPFromRCW_WinRT(Object objSrc, IntPtr pCPCMD,
IntPtr& ppTarget) at
Windows.UI.Xaml.UIElement.add_LosingFocus(TypedEventHandler`2 value) at
System.Runtime.InteropServices.WindowsRuntime.WindowsRuntimeMarshal.NativeOrStaticEventRegistrationImpl.AddEventHandler[T](Func`2
addMethod, Action`1 removeMethod, T handler)
at
System.Runtime.InteropServices.WindowsRuntime.WindowsRuntimeMarshal.AddEventHandler[T](Func`2
addMethod, Action`1 removeMethod, T handler)
_XamlTypeInfo.XamlUserType.ActivateInstance()
请帮助我解决该问题。
谢谢。
答案 0 :(得分:0)
LosingEvent
已在Windows API合约V4(Windows内部版本15063+)中引入。
在您的VM(14393)中使用的Windows版本上不可用。
中看到最低的OS / API合同版本。其他功能和要求
设备家族:Windows 10 Creators Update(引入的v10.0.15063.0)
API合同:Windows.Foundation.UniversalApiContract(第4版)
由于最小目标是Windows build 10240(API合约V1),因此需要在注册到此事件之前执行运行时检查。
if (ApiInformation.IsApiContractPresent("Windows.Foundation.UniversalApiContract"), 4, 0)
{
// This device supports all APIs in UniversalApiContract version 4.0
}
这是验证API是否可用的唯一方法。您将详细了解如何定位多个Windows版本here
如果该事件对于您想做的事情是绝对必要的,则需要将最低目标平台升级到15063。这样做,您的应用程序将不再能够在较早的OS版本上运行。