diff --git a/change/react-native-windows-3ffe8770-fba2-4468-a7a7-63d1d7257102.json b/change/react-native-windows-3ffe8770-fba2-4468-a7a7-63d1d7257102.json new file mode 100644 index 00000000000..03456184a3a --- /dev/null +++ b/change/react-native-windows-3ffe8770-fba2-4468-a7a7-63d1d7257102.json @@ -0,0 +1,7 @@ +{ + "type": "prerelease", + "comment": "add version checks for uses of XamlRoot", + "packageName": "react-native-windows", + "email": "julie@vendora.io", + "dependentChangeType": "patch" +} diff --git a/vnext/Microsoft.ReactNative/Modules/AlertModule.cpp b/vnext/Microsoft.ReactNative/Modules/AlertModule.cpp index e8208bd0d57..04de7fc2240 100644 --- a/vnext/Microsoft.ReactNative/Modules/AlertModule.cpp +++ b/vnext/Microsoft.ReactNative/Modules/AlertModule.cpp @@ -117,7 +117,9 @@ void Alert::ProcessPendingAlertRequestsXaml() noexcept { // https://github.com/microsoft/microsoft-ui-xaml/issues/2331 dialog.Opened([useXamlRootForThemeBugWorkaround](winrt::IInspectable const &sender, auto &&) { auto contentDialog = sender.as(); - auto popups = xaml::Media::VisualTreeHelper::GetOpenPopupsForXamlRoot(contentDialog.XamlRoot()); + auto popups = useXamlRootForThemeBugWorkaround + ? xaml::Media::VisualTreeHelper::GetOpenPopupsForXamlRoot(contentDialog.XamlRoot()) + : xaml::Media::VisualTreeHelper::GetOpenPopups(xaml::Window::Current()); auto contentAsFrameworkElement = useXamlRootForThemeBugWorkaround ? contentDialog.XamlRoot().Content().try_as() diff --git a/vnext/Microsoft.ReactNative/Views/FlyoutViewManager.cpp b/vnext/Microsoft.ReactNative/Views/FlyoutViewManager.cpp index 56927fef646..4abf1439681 100644 --- a/vnext/Microsoft.ReactNative/Views/FlyoutViewManager.cpp +++ b/vnext/Microsoft.ReactNative/Views/FlyoutViewManager.cpp @@ -200,7 +200,10 @@ void FlyoutShadowNode::createView(const winrt::Microsoft::ReactNative::JSValueOb } OnFlyoutClosed(GetViewManager()->GetReactContext(), m_tag, false); - m_xamlRootChangedRevoker.revoke(); + + if (Is19H1OrHigher()) { + m_xamlRootChangedRevoker.revoke(); + } } }); @@ -262,15 +265,17 @@ void FlyoutShadowNode::createView(const winrt::Microsoft::ReactNative::JSValueOb }); // Set XamlRoot on the Flyout to handle XamlIsland/AppWindow scenarios. - if (auto flyoutBase6 = m_flyout.try_as()) { - if (auto uiManager = GetNativeUIManager(GetViewManager()->GetReactContext()).lock()) { - if (auto xamlRoot = uiManager->tryGetXamlRoot(m_rootTag)) { - flyoutBase6.XamlRoot(xamlRoot); - m_xamlRootChangedRevoker = xamlRoot.Changed(winrt::auto_revoke, [this](auto &&, auto &&) { - if (m_isLightDismissEnabled) { - onDropViewInstance(); - } - }); + if (Is19H1OrHigher()) { + if (auto flyoutBase6 = m_flyout.try_as()) { + if (auto uiManager = GetNativeUIManager(GetViewManager()->GetReactContext()).lock()) { + if (auto xamlRoot = uiManager->tryGetXamlRoot(m_rootTag)) { + flyoutBase6.XamlRoot(xamlRoot); + m_xamlRootChangedRevoker = xamlRoot.Changed(winrt::auto_revoke, [this](auto &&, auto &&) { + if (m_isLightDismissEnabled) { + onDropViewInstance(); + } + }); + } } } } @@ -403,12 +408,16 @@ winrt::Flyout FlyoutShadowNode::GetFlyout() { void FlyoutShadowNode::OnShowFlyout() { AdjustDefaultFlyoutStyle(50000, 50000); if (m_isFlyoutShowOptionsSupported) { - if (!m_flyout.XamlRoot()) { - LogErrorAndClose("The target view window was closed before flyout could be shown."); - } else if (!m_targetElement && m_targetTag > 0) { - LogErrorAndClose("The target view unmounted before flyout could be shown."); - } else if (m_targetElement && m_flyout.XamlRoot() != m_targetElement.XamlRoot()) { - LogErrorAndClose("The target view window lost focus before flyout could be shown."); + if (Is19H1OrHigher()) { + if (!m_flyout.XamlRoot()) { + LogErrorAndClose("The target view window was closed before flyout could be shown."); + } else if (!m_targetElement && m_targetTag > 0) { + LogErrorAndClose("The target view unmounted before flyout could be shown."); + } else if (m_targetElement && m_flyout.XamlRoot() != m_targetElement.XamlRoot()) { + LogErrorAndClose("The target view window lost focus before flyout could be shown."); + } else { + m_flyout.ShowAt(m_targetElement, m_showOptions); + } } else { m_flyout.ShowAt(m_targetElement, m_showOptions); }