在c# WinRT应用程序中获取来自windows . ui . xml .dll的访问冲突

本文关键字:ui windows xml dll 访问冲突 WinRT 应用程序 获取 | 更新日期: 2023-09-27 18:13:28

我有一个WinRT应用程序,其中我有一个Button的页面。这个按钮打开了一个设置窗口。为了做到这一点,按钮的Click事件是异步的。

在Flyout的类中,我有一个异步Show()方法,它返回一个Task,很像MessageDialogs。该方法初始化一个ResultCompletionSource<object>和一个Popup。它打开Popup,然后返回ResultCompletionSource<T>.Task,这样它就可以等待了。

为了不创建多个窗格,我在按下按钮后立即将Button.IsEnabled设置为false,并在事件完成后返回true(并等待任务)。

我遇到的问题是,如果用户快速点击有问题的按钮,最终它会崩溃的应用程序,不可预测的。有时点击4次,有时点击20次。

我已经尝试捕捉异常并找出它在调试器中的位置。找到它需要使用Native调试器进行调试(好吧,我使用了Mixed)。该异常是win32 Access Violation异常,源于windows . ui . xml .dll。

是否有东西可能没有被封送回UI线程?具有Flyout作为ChildPopup具有LightDismissEnabled = true,这是(在此场景中)调用Closed事件的内容,该事件反过来设置TaskCompletionSource的Result。

描述问题的一些基本代码:

在CustomFlyout:

public Task<object> ShowAsync()
{
    resultCompletionSource = new TaskCompletionSource<object>();
    parentPopup = new Popup();
    //Set all of the height/width/bounds of the popup, along with setting the Closed Event.
    parentPopup.Child = this;
    parentPopup.IsOpen = true;
    return resultCompletionSource.Task;
}
void parentPopup_Closed(object sender, object e)
{
    if(resultCompletionSource != null)
    {
        resultCompletionSource.SetResult(null);
    }
    try
    {
        (sender as Popup).Closed -= parentPopup_closed;
    }
    catch{}
}

点击我的页面按钮:

private async void ShowFlyout_Button_Click(object sender, RoutedEventArgs e)
{
    ShowFlyout_Button.IsEnabled = false;
    try
    {
        CustomSettingsFlyout flyout = new CustomSettingsFlyout();
        await flyout.ShowAsync()
    }
    catch{}
    finally
    {
        ShowFlyout_Button.IsEnabled = false;
    }
}

我无法访问C元素源,但是调试器/反汇编器给了我这个堆栈跟踪:

>Windows.UI.Xaml.dll!CUIElement::GetRedirectionTransformsAndParentCompNode(CRenderTarget * pRenderTarget, TransformAndClipStack * pTransformsToParentCompNode, CMILMatrix * pTransformToRoot, unsigned int * pIsTransformToRootAnimating, unsigned int * pAreAllAncestorsVisible, HWCompTreeNode * * ppNearestCompNode) Line 7462   C++
Windows.UI.Xaml.dll!CUIElement::GetRedirectionTransformsAndParentCompNode(CRenderTarget * pRenderTarget, TransformAndClipStack * pTransformsToParentCompNode, CMILMatrix * pTransformToRoot, unsigned int * pIsTransformToRootAnimating, unsigned int * pAreAllAncestorsVisible, HWCompTreeNode * * ppNearestCompNode) Line 7495    C++
Windows.UI.Xaml.dll!HWWalk::RenderProperties(CUIElement * pUIElement, const HWRenderParams & parentRP, D2DRenderContext & d2dRenderContext, unsigned int requiresRedirectedDrawing, unsigned int * pSkipRenderWhileInheritedCollapsed, unsigned int * pSkipRenderWhileTransparent, unsigned int * pSkipRenderWhileClippedOut, unsigned int * pSkipRenderWhileLayoutClippedOut, unsigned int * pSkipRenderWhileTransformTooSmall) Line 441   C++
Windows.UI.Xaml.dll!HWWalk::Render(CUIElement * pUIElement, const HWRenderParams & parentRP, D2DRenderContext & d2dRenderContext, unsigned int redirectedDraw) Line 199 C++
Windows.UI.Xaml.dll!HWWalk::RenderContentAndChildren(CUIElement * pUIElement, HWRenderParams & myRP, D2DRenderContext & d2dRenderContext, unsigned int requiresRedirectedDrawing, unsigned int elementHasCompNode) Line 3461    C++
Windows.UI.Xaml.dll!HWWalk::RenderProperties(CUIElement * pUIElement, const HWRenderParams & parentRP, D2DRenderContext & d2dRenderContext, unsigned int requiresRedirectedDrawing, unsigned int * pSkipRenderWhileInheritedCollapsed, unsigned int * pSkipRenderWhileTransparent, unsigned int * pSkipRenderWhileClippedOut, unsigned int * pSkipRenderWhileLayoutClippedOut, unsigned int * pSkipRenderWhileTransformTooSmall) Line 629   C++
Windows.UI.Xaml.dll!HWWalk::Render(CUIElement * pUIElement, const HWRenderParams & parentRP, D2DRenderContext & d2dRenderContext, unsigned int redirectedDraw) Line 199 C++
Windows.UI.Xaml.dll!HWWalk::RenderContentAndChildren(CUIElement * pUIElement, HWRenderParams & myRP, D2DRenderContext & d2dRenderContext, unsigned int requiresRedirectedDrawing, unsigned int elementHasCompNode)  C++
Windows.UI.Xaml.dll!HWWalk::RenderProperties(CUIElement * pUIElement, const HWRenderParams & parentRP, D2DRenderContext & d2dRenderContext, unsigned int requiresRedirectedDrawing, unsigned int * pSkipRenderWhileInheritedCollapsed, unsigned int * pSkipRenderWhileTransparent, unsigned int * pSkipRenderWhileClippedOut, unsigned int * pSkipRenderWhileLayoutClippedOut, unsigned int * pSkipRenderWhileTransformTooSmall) Line 629   C++
Windows.UI.Xaml.dll!HWWalk::Render(CUIElement * pUIElement, const HWRenderParams & parentRP, D2DRenderContext & d2dRenderContext, unsigned int redirectedDraw) Line 199 C++
Windows.UI.Xaml.dll!HWWalk::RenderContentAndChildren(CUIElement * pUIElement, HWRenderParams & myRP, D2DRenderContext & d2dRenderContext, unsigned int requiresRedirectedDrawing, unsigned int elementHasCompNode) Line 3461    C++
Windows.UI.Xaml.dll!HWWalk::RenderProperties(CUIElement * pUIElement, const HWRenderParams & parentRP, D2DRenderContext & d2dRenderContext, unsigned int requiresRedirectedDrawing, unsigned int * pSkipRenderWhileInheritedCollapsed, unsigned int * pSkipRenderWhileTransparent, unsigned int * pSkipRenderWhileClippedOut, unsigned int * pSkipRenderWhileLayoutClippedOut, unsigned int * pSkipRenderWhileTransformTooSmall) Line 629   C++
Windows.UI.Xaml.dll!HWWalk::Render(CUIElement * pUIElement, const HWRenderParams & parentRP, D2DRenderContext & d2dRenderContext, unsigned int redirectedDraw) Line 199 C++
Windows.UI.Xaml.dll!HWWalk::RenderRoot(CUIElement * pVisualRoot, CRenderTarget * pRenderTarget, HWInstance * pPersistentRenderDataSentinel, HWCrossFrameResources * pCrossFrameResources, HWFrameData * pPreviousFrameData, unsigned int backgroundColor, unsigned int forceRedraw, unsigned int needsToReleaseHardwareResources, unsigned int isOverdrawHeatMapEnabled, TransformAndClipStack * pPrependTransformAndClip, const CMILMatrix & displayOrientationTransform, const CMILMatrix & displayOrientationTransformInverse, unsigned int forceAllSlotsDirty, HWCompTreeNode * * ppRootNode, BrushCloneList * * ppBrushCloneList, unsigned int * pHasRenderedContent) Line 3847    C++
Windows.UI.Xaml.dll!CCoreServices::RenderWalk(HWWalk * pHWWalk, ICoreRenderTarget * pIRenderTarget, VisualTree * pVisualTree, CSwapChainBackgroundPanel * pSCBP, unsigned int uBackgroundColor, unsigned int forceRedraw, unsigned int needsToReleaseHardwareResources, unsigned int isOverdrawHeatMapEnabled, unsigned int forceAllSlotsDirty) Line 11478  C++
Windows.UI.Xaml.dll!CCoreServices::NWDrawTree(HWWalk * pHWWalk, ICoreRenderTarget * pIRenderTarget, VisualTree * pVisualTree, unsigned int uBackgroundColor, unsigned int forceRedraw, unsigned int needsToReleaseHardwareResources, XRECT_WH * prcDirtyRect) Line 10985    C++
Windows.UI.Xaml.dll!CCoreServices::NWDrawMainTree(ICoreRenderTarget * pIRenderTarget, unsigned int fForceRedraw, unsigned int needsToReleaseHardwareResources, XRECT_WH * prcDirtyRect) Line 10697  C++
Windows.UI.Xaml.dll!CWindowRenderTarget::Draw(ICoreServices * pCore, unsigned int fForceRedraw, XRECT_WH * prcDirtyRect) Line 128   C++
Windows.UI.Xaml.dll!CXcpBrowserHost::OnTick() Line 1160 C++
Windows.UI.Xaml.dll!CXcpDispatcher::Tick() Line 1120    C++
Windows.UI.Xaml.dll!CJupiterControl::HandleWindowMessage(unsigned int uMsg, unsigned int wParam, long lParam) Line 249  C++
Windows.UI.Xaml.dll!CJupiterWindow::WindowProc(HWND__ * hwnd, unsigned int uMsg, unsigned int wParam, long lParam) Line 354 C++
Windows.UI.Xaml.dll!CJupiterWindow::StaticWindowProc(HWND__ * hwnd, unsigned int uMsg, unsigned int wParam, long lParam) Line 208   C++
user32.dll!_InternalCallWinProc@20()    Unknown
user32.dll!_UserCallWinProcCheckWow@36()    Unknown
user32.dll!_DispatchClientMessage@24()  Unknown
user32.dll!___fnDWORD@4()   Unknown
ntdll.dll!_KiUserCallbackDispatcher@12()    Unknown
Windows.UI.dll!Windows::UI::Core::CDispatcher::ProcessMessage(int bDrainQueue, int * pbAnyMessages) Line 110    C++
Windows.UI.dll!Windows::UI::Core::CDispatcher::ProcessEvents(Windows::UI::Core::CoreProcessEventsOption options) Line 184   C++
Windows.UI.Xaml.dll!CJupiterWindow::RunCoreWindowMessageLoop() Line 416 C++
Windows.UI.Xaml.dll!CJupiterControl::RunMessageLoop() Line 714  C++
Windows.UI.Xaml.dll!DirectUI::DXamlCore::RunMessageLoop() Line 2538 C++
Windows.UI.Xaml.dll!DirectUI::FrameworkView::Run() Line 91  C++
twinapi.dll!`Windows::ApplicationModel::Core::CoreApplicationViewAgileContainer::RuntimeClassInitialize'::`55'::<lambda_A2234BA2CCD64E2C>::operator()(void * pv) Line 560   C++
twinapi.dll!`Windows::ApplicationModel::Core::CoreApplicationViewAgileContainer::RuntimeClassInitialize'::`55'::<lambda_A2234BA2CCD64E2C>::<helper_func>(void * pv) Line 613    C++
SHCore.dll!_SHWaitForThreadWithWakeMask@12()    Unknown
kernel32.dll!@BaseThreadInitThunk@12()  Unknown
ntdll.dll!___RtlUserThreadStart@8() Unknown
ntdll.dll!__RtlUserThreadStart@8()  Unknown

我唯一能想到的是,我应该在某个地方用CoreDispather.Invoke手动编组对UI线程的调用,但我不知道在哪里。

如果任何MSFT-y有源代码,并希望查看导致问题的文件,调试器将指向d:'win8_gdr'windows'dxaml'xcp'core'core'elements'uielement.cpp,这显然不在我的系统上。

在c# WinRT应用程序中获取来自windows . ui . xml .dll的访问冲突

我似乎已经找到了答案,从SettingsFlyout的例子(但不是弹出的例子)。

SettingsFlyout示例让您添加

parentPopup.Closed += OnPopupClosed;
Window.Current.Activated += OnWindowActivated;

到Popup,方法为

private void OnWindowActivated(object sender, Windows.UI.Core.WindowActivatedEventArgs e)
    {
        if (e.WindowActivationState == Windows.UI.Core.CoreWindowActivationState.Deactivated)
        {
            parentPopup.IsOpen = false;
        }
    }
void OnPopupClosed(object sender, object e)
    {
        Window.Current.Activated -= OnWindowActivated;
    }

这似乎已经修复了崩溃。不管我怎么使劲地按"显示弹出框"按钮,它都无法再现