如何在窗口中心定位弹出窗口(Windows Store应用程序)

10

我有一个自定义弹出窗口(作为用户控件),我在程序中进行加载。我无法在x轴上居中它,只能在垂直方向上居中。弹出窗口没有添加到xaml文件中,但是已经添加到根窗口中。

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using System.Windows;
using Windows.UI.Core;

// The User Control item template is documented at http://go.microsoft.com/fwlink/?LinkId=234236

namespace QSTLibrary.WIN8.Tools
{
    public sealed partial class CustomProgressRingPopup : UserControl
    {

        public CustomProgressRingPopup()
        {
            this.InitializeComponent();
        }

        public string Text
        {
            get { return (string)GetValue(TextProperty); }
            set { SetValue(TextProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Text.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TextProperty = DependencyProperty.Register(
                        "Text", 
                        typeof(string), 
                        typeof(CustomProgressRingPopup),
                        new PropertyMetadata("", OnTextChanged));


        public void OpenPopup()
        {
            this.ParentPopup.IsOpen = true;
        }

        public void ClosePopup()
        {
            this.ParentPopup.IsOpen = false;
        }

        private static void OnTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var instance = d as CustomProgressRingPopup;
            var newValue = e.NewValue as string;
            if (instance != null && newValue != null)
            {
                instance.CustomTextBlock.Text = newValue;
            }
        }

        private void OnPopupLoaded(object sender, RoutedEventArgs e)
        {
            this.ParentPopup.HorizontalOffset = (Window.Current.Bounds.Width - gdChild.ActualWidth) / 2;
            this.ParentPopup.VerticalOffset = (Window.Current.Bounds.Height - gdChild.ActualHeight) / 2;
        }
    }
}

用户控件 xaml:

<UserControl
    x:Class="QSTLibrary.WIN8.Tools.CustomProgressRingPopup"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:QSTLibrary.WIN8.Tools"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300"
    d:DesignWidth="400">

    <Popup x:Name="ParentPopup" HorizontalAlignment="Center" VerticalAlignment="Center" Loaded="OnPopupLoaded">
        <Grid x:Name="gdChild" Height="auto" Width="auto" Background="Blue" Margin="20">
            <Grid.RowDefinitions>
                <RowDefinition/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition/>
                <ColumnDefinition/>
            </Grid.ColumnDefinitions>
            <ProgressRing x:Name="CustomProgressRing" Height="40" Width="40" IsActive="true" Grid.Column="0" Margin="20"/>
            <TextBlock x:Name="CustomTextBlock" Height="auto" Width="auto" FontSize="25" Grid.Column="1" Margin="20"/>
        </Grid>
    </Popup>    
</UserControl>

这是我如何在外部使用它:

_loginProgressRingPopup.Text = "Logging in";
_loginProgressRingPopup.OpenPopup();
4个回答

15

我已在cs文件中添加了弹出窗口的LayoutUpdate回调函数,现在可以正常工作了。

private void OnLayoutUpdated(object sender, object e)
        {
            if(gdChild.ActualWidth == 0 && gdChild.ActualHeight == 0)
            {
                return;
            }

            var coordinates = ParentPopup.TransformToVisual(Window.Current.Content).TransformPoint(new Point(0, 0));            

            double ActualHorizontalOffset = ParentPopup.HorizontalOffset;
            double ActualVerticalOffset = ParentPopup.VerticalOffset;

            double NewHorizontalOffset = ((Window.Current.Bounds.Width - gdChild.ActualWidth) / 2) - coordinates.X;
            double NewVerticalOffset = ((Window.Current.Bounds.Height - gdChild.ActualHeight) / 2) - coordinates.Y;

            if (ActualHorizontalOffset != NewHorizontalOffset || ActualVerticalOffset != NewVerticalOffset)
            {
                this.ParentPopup.HorizontalOffset = NewHorizontalOffset;
                this.ParentPopup.VerticalOffset = NewVerticalOffset;
            }
        }

以及来自XAML的弹出窗口:

<Popup x:Name="ParentPopup" LayoutUpdated="OnLayoutUpdated">

“Window.Current.Bounds”导致我的应用程序崩溃,并显示错误消息“检测到布局循环。无法完成布局。”通过将其更改为我正在使用的顶级容器,问题得到解决。我还更改了(NewHorizontalOffset != double.NaN && (ActualHorizontalOffset != NewHorizontalOffset || ActualVerticalOffset != NewVerticalOffset)) - 以检查是否出现NaN。 - Dave Friedel
@DaveFriedel 我们正在面临同样的问题,但无法实施您的建议。我们有一个弹出窗口,在其中我们使用了一个用户控件,该控件具有此代码,我们应该如何解决该问题,以便不会抛出循环布局异常。 - Teja
@Teja - 你能发一个示例吗(我不太愿意问这个,因为通常很复杂)我建议将"Window.Current.Bounds.Width"替换为栈顶的网格根或其他对象的名称。我发现"Window"引起了我的问题,但如果我给一个包装对象命名,它就可以工作。 - Dave Friedel
似乎Windows不喜欢在LayoutUpdated处理程序中调整影响布局的属性。我建议改用SizeChanged事件,这个事件似乎没有这个问题。 - devios1
1
也遇到了“检测到布局循环。无法完成布局”的问题,但通过将NewHorizontal和Vertical偏移量转换为int来解决了这个问题。 - duo

3
在UWP Windows Store应用程序中定位弹出窗口并不像可能的那么直接。这是我最终解决它的方法。不要观察LayoutUpdated事件(这可能会导致布局循环),而是观察视图顶层元素的SizeChanged事件,并使用它来重新定位包含在其中的弹出窗口。
在我的情况下,我将对话框定位在窗口中心,因此使用Window.Current。您还需要转换坐标,因为弹出窗口将使用其自己的相对坐标系统,该系统基于Popup元素在布局中实际定义的位置。
private void MyDialog_SizeChanged(object sender, SizeChangedEventArgs e) {
    var transform = Window.Current.Content.TransformToVisual(_popup);
    Point point = transform.TransformPoint(new Point(0, 0)); // gets the window's (0,0) coordinate relative to the popup

    double hOffset = (Window.Current.Bounds.Width - this.ActualWidth) / 2;
    double vOffset = (Window.Current.Bounds.Height - this.ActualHeight) / 2;

    _popup.HorizontalOffset = point.X + hOffset;
    _popup.VerticalOffset = point.Y + vOffset;
}

0

你可以在XAML文件中居中:

<Popup x:Name="ParentPopup" PlacementTarget="{Binding ElementName=MainPanel}" Placement="Center" />

1
我遇到了错误:在类型“Popup”中找不到属性“PlacementTarget”。'Placement'也是同样的情况。 - Alexandru Circus
@AlexandruCircus PlacementTarget 是您想要居中的面板。MainPanel 默认存在,但您也可以在其他面板中进行居中。 - Butzke
1
好的,我理解了。但是从错误信息“在类型'Popup'中找不到属性'PlacementTarget'”来看,似乎无法将PlacementTarget属性应用于Popup元素。此外,在我编写Placemen...时,它无法自动完成代码,它应该提供PlacementTarget的自动完成建议。 - Alexandru Circus
该项目是一个Windows Store应用程序。也许在这种类型的项目中,PlacementTarget不可用。我是C#、.NET的新手……我是一名Java程序员。 - Alexandru Circus
我认为Windows商店应用程序的弹出窗口没有这个属性。http://msdn.microsoft.com/library/windows/apps/br227842 - Alexandru Circus
显示剩余2条评论

-1

我修改了Butzke的答案,使其更具普适性:

<Popup PlacementTarget="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}" Placement="Center" />

2
这适用于WPF,但在UWP应用程序中,PlacementTarget和FindAncestor Mode不可用。 - Strickos9

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接