ContentDialog 未对齐到 UWP 的中心 [英] ContentDialog not aligning to center on UWP
问题描述
据我所知,ContentDialog
的默认行为应该是让它以 PC 为中心并在移动设备上对齐到顶部,但在我的情况下,即使在 PC 上我也将它对齐到顶部并且我不明白发生了什么.
From what I know, ContentDialog
's default behavior should be to have it centered on PC and aligned to top on mobile, but in my case I have it aligned to top even on PC and I don't understand what's going on.
我正在使用代码隐藏创建它,这是我正在使用的代码片段:
I'm creating it using code-behind, and this is a snippet of the code that I'm using:
// Creates the password box
var passwordBox = new PasswordBox {IsPasswordRevealButtonEnabled = true, Margin = new Thickness(5)};
// Creates the StackPanel with the content
var contentPanel = new StackPanel();
contentPanel.Children.Add(new TextBlock
{
Text = "Insert your password to access the application",
Margin = new Thickness(5),
TextWrapping = TextWrapping.WrapWholeWords
});
contentPanel.Children.Add(passwordBox);
// Creates the password dialog
_passwordDialog = new ContentDialog
{
PrimaryButtonText = "accept",
IsPrimaryButtonEnabled = false,
SecondaryButtonText = "exit",
Title = "Authentication",
Content = contentPanel
};
// Report that the dialog has been opened to avoid overlapping
_passwordDialog.Opened += (s, e) =>
{
// HACK - opacity set to 0 to avoid seeing behind dialog
Window.Current.Content.Opacity = 0;
_canShowPasswordDialog = false;
};
// Report that the dialog has been closed to enable it again
_passwordDialog.Closed += (s, e) =>
{
// HACK - opacity set to 1 to reset the window to original options
Window.Current.Content.Opacity = 1;
_canShowPasswordDialog = true;
};
// Clear inserted password for next logins
_passwordDialog.PrimaryButtonClick += (s, e) =>
{
// ... login ...
};
// Close the app if the user doesn't insert the password
_passwordDialog.SecondaryButtonClick += (s, e) => { BootStrapper.Current.Exit(); };
// Set the binding to enable/disable the accept button
_passwordDialog.SetBinding(ContentDialog.IsPrimaryButtonEnabledProperty, new Binding
{
Source = passwordBox,
Path = new PropertyPath("Password"),
Mode = BindingMode.OneWay,
Converter = new PasswordValidatorConverter()
});
我已经尝试使用 VerticalAlignment
和 FullSizeDesired
但我没有得到预期的结果.
I already tried using VerticalAlignment
and FullSizeDesired
but I don't get the expected results.
我该如何解决这个问题?
How can I fix this?
推荐答案
ContentDialog
就像 Popup
控件,PopupRoot
持有它当它显示在页面上时.但是与Popup
控件不同的是,ContentDialog
的放置位置是写在后面的代码中的,而且这个属性是不暴露给我们的,我们无法更改.
The ContentDialog
is like the Popup
control, the PopupRoot
holds it when it is shown on the page. But unlike the Popup
control, the location where to place the ContentDialog
is written in the code behind, and this property is not exposed to us, we can not change it.
据我所知,ContentDialog 的默认行为应该是让它以 PC 为中心.
From what I know, ContentDialog's default behavior should be to have it centered on PC.
ContentDialog
并不总是以 PC 为中心.我根据您发布的代码测试了 ContentDialog
.ContentDialog
当页面高度小于640时对齐到页面顶部.当页面高度等于640或大于640时它居中.
The ContentDialog
is not always centered on PC. I test the ContentDialog
bases on the code from you posted. The ContentDialog
is aligned to top of the page when the page height smaller than 640. It is centered of the page when the page height equal to 640 or larger than 640.
从上图我们可以看出,放置ContentDialog
的位置是由Page
的高度触发的.
From the image above we can see that the location where to place the ContentDialog
is triggered by the height of the Page
.
这篇关于ContentDialog 未对齐到 UWP 的中心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!