Skip to content

Commit

Permalink
Fix xaml islands popup position bug
Browse files Browse the repository at this point in the history
Fix xaml islands popup position bug
  • Loading branch information
Gaoyifei1011 committed Nov 28, 2024
1 parent 5f20775 commit 482b8c6
Showing 1 changed file with 8 additions and 28 deletions.
36 changes: 8 additions & 28 deletions WindowsTools/Views/Windows/MainWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -196,39 +196,20 @@ protected override void OnStyleChanged(EventArgs args)
protected override void OnMove(EventArgs args)
{
base.OnMove(args);
if ((Content as FrameworkElement).XamlRoot is not null)
if (Content is not null && Content.XamlRoot is not null)
{
IReadOnlyList<Popup> popupRootList = VisualTreeHelper.GetOpenPopupsForXamlRoot((Content as FrameworkElement).XamlRoot);
foreach (Popup popupRoot in popupRootList)
// 窗口移动时,校对并纠正弹出窗口位置错误的问题
foreach (Popup popup in VisualTreeHelper.GetOpenPopupsForXamlRoot(Content.XamlRoot))
{
// 关闭浮出控件
if (popupRoot.Child is FlyoutPresenter flyoutPresenter && !flyoutPresenter.Name.Equals("DialogFlyout"))
{
popupRoot.IsOpen = false;
}

// 关闭菜单浮出控件
if (popupRoot.Child as MenuFlyoutPresenter is not null)
{
popupRoot.IsOpen = false;
}

// 关闭日期选择器浮出控件
if (popupRoot.Child as DatePickerFlyoutPresenter is not null)
{
popupRoot.IsOpen = false;
}

// 关闭时间选择器浮出控件
if (popupRoot.Child as TimePickerFlyoutPresenter is not null)
{
popupRoot.IsOpen = false;
}
ElementCompositeMode compositeMode = popup.CompositeMode;
popup.CompositeMode = compositeMode is ElementCompositeMode.SourceOver ? ElementCompositeMode.MinBlend : ElementCompositeMode.SourceOver;
popup.CompositeMode = compositeMode;
}
}

// 修改托管 CoreWindow 窗口的大小和位置
Form CoreWindowHostWindowForm = typeof(XamlApplicationExtensions).GetField("CoreWindowHostWindow", BindingFlags.NonPublic | BindingFlags.Static).GetValue(null) as Form;

CoreWindowHostWindowForm.Location = Location;
CoreWindowHostWindowForm.Size = Size;
}
Expand All @@ -247,8 +228,7 @@ protected override void OnSizeChanged(EventArgs args)

if (Content.XamlRoot is not null)
{
IReadOnlyList<Popup> PopupRoot = VisualTreeHelper.GetOpenPopupsForXamlRoot(Content.XamlRoot);
foreach (Popup popupRoot in PopupRoot)
foreach (Popup popupRoot in VisualTreeHelper.GetOpenPopupsForXamlRoot(Content.XamlRoot))
{
// 关闭内容对话框
if (popupRoot.Child as ContentDialog is not null)
Expand Down

0 comments on commit 482b8c6

Please sign in to comment.