Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ContentDialog未对齐到UWP上的中心_C#_Win Universal App_Windows 10_Windows 10 Universal - Fatal编程技术网

C# ContentDialog未对齐到UWP上的中心

C# ContentDialog未对齐到UWP上的中心,c#,win-universal-app,windows-10,windows-10-universal,C#,Win Universal App,Windows 10,Windows 10 Universal,据我所知,ContentDialog的默认行为应该是以PC为中心,在移动设备上与top对齐,但就我而言,即使在PC上也与top对齐,我不知道发生了什么 我正在使用代码隐藏创建它,这是我正在使用的代码片段: // Creates the password box var passwordBox = new PasswordBox {IsPasswordRevealButtonEnabled = true, Margin = new Thickness(5)}; // Cr

据我所知,
ContentDialog
的默认行为应该是以PC为中心,在移动设备上与top对齐,但就我而言,即使在PC上也与top对齐,我不知道发生了什么

我正在使用代码隐藏创建它,这是我正在使用的代码片段:

// 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()
});
我已经尝试过使用
垂直对齐
FullSizeDesired
,但没有得到预期的结果


如何解决此问题?

内容对话框类似于
弹出控件,当它显示在页面上时,
弹出控件将其保留。但是与
弹出窗口
控件不同,放置
内容对话框
的位置写在后面的代码中,并且此属性不向我们公开,我们无法更改它

据我所知,ContentDialog的默认行为应该是以PC为中心


ContentDialog
并不总是以PC为中心。我根据您发布的代码测试
ContentDialog
。当页面高度小于640时,
ContentDialog
与页面顶部对齐。当页面高度等于640或大于640时,它位于页面中心

从上图中,我们可以看到放置
内容对话框的位置是由
页面的高度触发的

  • 使用placement参数显示对话框,如下所示:dialog.showsync(ContentDialogPlacement.InPlace)
  • 将内容对话框实例化为应用程序布局的一部分,例如,在应用程序类中放置类似的内容:

    private Grid RootGrid => (Grid)Window.Current.Content;
    private Frame Frame => this.RootGrid?.Children[0] as Frame;
    private ContentDialog ContentDialog => this.RootGrid?.Children[1] as ContentDialog;
    
    protected override void OnLaunched(LaunchActivatedEventArgs e)
    {
        if (Window.Current.Content == null) {
            Window.Current.Content = new Grid();
            Debug.Assert( this.RootGrid != null);
            this.RootGrid.Children.Add(new Frame()); 
            this.RootGrid.Children.Add(new ContentDialog());
            Debug.Assert(this.Frame != null && this.ContentDialog != null);
        }
        ...
        Window.Current.Activate();
    }
    
  • 将内容对话框的水平对齐、垂直对齐、MaxWidth和MaxHeight保留为默认值,以使对话框的“模式背景”覆盖应用程序的整个区域


  • 注意:有关确保一次只显示一个内容对话框的提示,请参见

    我不知道这里的细节,但似乎是设置对话框的MaxWidth或MaxHeight导致了问题

    我将Maxwidth设置为500,对话框最终显示在屏幕的左侧。我将MaxWidth更改为600,对话框仍然位于左侧,但不太远

    然后我删除了MaxWidth,对话框回到了屏幕中央

    大概对话框是通过某种覆盖创建的,为了正确对齐对话框,覆盖必须是全屏大小。设置MaxWidth或MaxHeight时,将设置覆盖的宽度/高度,而不是对话框本身。因此,如果这些值小于实际屏幕大小,则叠加在屏幕的一侧(向上和左手边),对话框位于叠加的中间。

    删除MaxWidth为我修复了这个问题

    要获得所需的宽度,不要在对话框本身上设置MaxWidth,而是在告诉对话框保持的内容上设置MaxWidth


    在仔细研究其他答案后,我注意到sjb sjb也暗示了这一点。我不明白他回答中的其他代码是干什么的。只要删除MaxWidth/Height,问题就应该得到解决。

    哦,好吧,这是否意味着没有机会强制它在pc上居中?上面看起来真难看!您可以添加
    ApplicationView.PreferredLaunchViewSize=新大小(980800);ApplicationView.PreferredLaunchWindowingMode=ApplicationViewWindowingMode.PreferredLaunchViewSize使页面高度等于800。这对手机有影响吗?有没有办法为应用程序窗口设置最小高度?这对手机没有影响,因为手机不支持调整页面大小。在pc中,似乎没有设置最小高度的方法。所有的应用程序都可以调整页面大小。Jayden Gu的回复并不能解决这个问题。请参阅我在下面发布的答案@StepTNT您被邀请重新指定“答案”复选标记。您找到任何解决方案或解决方法了吗?