我已经使用下面的代码创建一个弹出,但我无法弄清楚如何居中
我试图自动改变运行时的保证金,但我无法弄清楚如何做到这一点,但做任何人有如何中心弹出的想法?
它不具有标准尺寸的原因,我需要我的全球化方案
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}" Name="MainGrid">
<Popup x:Uid="LoginPopup" IsOpen="True" Name="LoginPopup">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBlock Margin="10" Grid.Column="0" Grid.Row="0" Text="App Name" Grid.ColumnSpan="2" Style="{StaticResource HeaderTextStyle}" />
<TextBlock Margin="10" Grid.Column="0" Grid.Row="1" Text="Username" Style="{StaticResource ResourceKey=SubheaderTextStyle}" />
<TextBox Margin="10" Grid.Column="1" Grid.Row="1" Name="InputUsername" />
<TextBlock Margin="10" Grid.Column="0" Grid.Row="2" Text="Password" Style="{StaticResource ResourceKey=SubheaderTextStyle}" />
<PasswordBox Margin="10" Grid.Column="1" Grid.Row="2" Name="InputPassword" />
<StackPanel Margin="10" Grid.Column="1" Grid.Row="3" HorizontalAlignment="Left" Orientation="Horizontal">
<Button Name="Login" x:Uid="LoginPopupLogin" />
<Button Name="Cancel" x:Uid="LoginPopupCancel" />
</StackPanel>
</Grid>
</Popup>
</Grid>
UPDATE
我试着用下面user1603313的答案,但它没有这样做的伎俩,因为它说的弹出式内网格的尺寸为NaN。
我也试过的方法转移到网格,但它并没有这样的伎俩无论是
我说的方法是这样的与电网正确地更新
private void LoginPopup_Loaded_1(object sender, RoutedEventArgs e)
{
LoginPopup.HorizontalOffset = (Window.Current.Bounds.Width - gdChild.ActualWidth) / 2;
LoginPopup.VerticalOffset = (Window.Current.Bounds.Height - gdChild.ActualHeight) / 2;
}
下面是你的问题的解决方案。 我正在重写XAML代码,并伴随着修改,你可以找到的代码之后的解释。
<Popup x:Uid="LoginPopup" IsOpen="True" Name="LoginPopup" Loaded="LoginPopup_Loaded_1">
<Grid Background="Red" x:Name="gdChild" Height="Auto" Width="Auto">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBlock Margin="10" Grid.Column="0" Grid.Row="0" Text="App Name" Grid.ColumnSpan="2" Style="{StaticResource HeaderTextStyle}" />
<TextBlock Margin="10" Grid.Column="0" Grid.Row="1" Text="Username" Style="{StaticResource ResourceKey=SubheaderTextStyle}" />
<TextBox Margin="10" Grid.Column="1" Grid.Row="1" Name="InputUsername" />
<TextBlock Margin="10" Grid.Column="0" Grid.Row="2" Text="Password" Style="{StaticResource ResourceKey=SubheaderTextStyle}" />
<PasswordBox Margin="10" Grid.Column="1" Grid.Row="2" Name="InputPassword" />
<StackPanel Margin="10" Grid.Column="1" Grid.Row="3" HorizontalAlignment="Left" Orientation="Horizontal">
<Button Name="Login" x:Uid="LoginPopupLogin" />
<Button Name="Cancel" x:Uid="LoginPopupCancel" />
</StackPanel>
</Grid>
</Popup>
在这里,我添加了一个事件Loaded="LoginPopup_Loaded_1"
以弹出窗口的XAML
下面是在C#中的事件代码
private void LoginPopup_Loaded_1(object sender, RoutedEventArgs e)
{
LoginPopup.HorizontalOffset = (Window.Current.Bounds.Width - gdChild.ActualWidth) / 2;
LoginPopup.VerticalOffset = (Window.Current.Bounds.Height - gdChild.ActualHeight) / 2;
}
说明:
HorizontalOffset获取应用程序窗口的左侧和弹出窗口的左侧之间的距离。
类似地垂直偏移得到窗口的顶部和顶部弹出之间的距离
既然我们已经居中对齐,所以我们必须减去宽度的一半,并从宽度弹出窗口的高度和应用程序窗口的高度(弹出窗口的中心是弹出窗口的一半的距离,它的顶部和左侧边界)
该代码被写入Loaded="LoginPopup_Loaded_1"
事件,因为当元件在应用程序窗口被呈现和网格被取,因为它是所有子元素的容器网格被称为此事件。
我希望中号清晰:)
回复:而且是有可能加载运行,当屏幕旋转时? - The87Boy 24分钟前
答案是肯定的,你可以通过创建一个方法执行同一套代码,并调用该方法Window.Current.SizeChanged += Current_SizeChanged;
void Current_SizeChanged(object sender, Windows.UI.Core.WindowSizeChangedEventArgs e)
{
//call custom method from loaded event as well as size changed event
}
关于x:名称和名称属性ü问我会解释将是一样写在下面的链接是什么
X:名称和名称差异的Silverlight
而且通过这个链接,WPF
在WPF中,什么是X之间的差异:名称和名称属性?
按我的一般理解的名称是类的属性(例如:网格或TextBlock的,还有更多),但需要访问一些不具有name属性类如何(例如:故事板等),以便为目的X:提供名称。 在InitializeComponent方法调用X:名称的和名称映射到使用类FindName(string)
方法让他们成为访问。
现在什么内部发生是一个很长的故事缩短
MainPage.xaml中 - > MainPage.gics - > MainPage.xaml.cs中