gpt4 book ai didi

c# - 如何在窗口中居中弹出窗口(Windows 商店应用程序)

转载 作者:可可西里 更新时间:2023-11-01 09:03:05 24 4
gpt4 key购买 nike

我有一个以编程方式加载的自定义弹出窗口(作为用户控件)。我无法将它置于 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();

最佳答案

我已经在 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">

关于c# - 如何在窗口中居中弹出窗口(Windows 商店应用程序),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19032750/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com