繁体   English   中英

为什么堆栈面板忽略方向属性?

[英]Why does the stackpanel ignore Orientation property?

我想在 ContentDialog 中使用带有两个 uri 的两个文本块的堆栈面板。 问题是,尽管我将属性设置为 Vertical,但它没有效果,这就是视觉效果

ContentDialog 与混乱的 StackPanel

private async void AboutButton_Click(object sender, RoutedEventArgs e)
    {       
        TextBlock gHRepoTB = new TextBlock();
        Hyperlink hyperlink1 = new Hyperlink();
        Run run1 = new Run();
        run1.Text = "View GitHub repository";
        hyperlink1.NavigateUri = new Uri("https://www.google.com/");
        hyperlink1.Inlines.Add(run1);
        gHRepoTB.Inlines.Add(hyperlink1);
        TextBlock privacyPolicyTB = new TextBlock();
        Hyperlink hyperlink2 = new Hyperlink();
        Run run2 = new Run();
        run2.Text = "Privacy Policy";
        hyperlink2.NavigateUri = new Uri("https://www.bing.com/");
        hyperlink2.Inlines.Add(run2);
        gHRepoTB.Inlines.Add(hyperlink2);
        StackPanel aboutPanel = new StackPanel();
        aboutPanel.Orientation = Orientation.Vertical;
        aboutPanel.Children.Add(gHRepoTB);
        aboutPanel.Children.Add(privacyPolicyTB);
        ContentDialog aboutDialog = new ContentDialog();            
        aboutDialog.Title = "About";
        aboutDialog.Content = aboutPanel;
        aboutDialog.PrimaryButtonText = "Report a bug";
        aboutDialog.PrimaryButtonClick += ReportBug_Click;
        aboutDialog.PrimaryButtonStyle = App.Current.Resources["AccentButtonStyle"] as Style;
        aboutDialog.CloseButtonText = "Close";
        await aboutDialog.ShowAsync();
    }

@mm8 展示了如何解决您的问题,但这是不必要且不清楚的代码的结果。

以下是我更改的内容:

  • 我已将具有超链接布局的按钮更改为hyperlinkbutton
  • 我已经将内容设置为一个字符串,因为在这种情况下不再需要。
  • 我没有为主按钮手动设置样式,而是指定了DefaultButton

通过以这种方式创建对话框,您不太容易犯错误,其他人和您以后将能够更快地了解正在发生的事情,并且引入的临时变量也更少。

代码(您应该能够直接将此代码复制到当前的事件处理程序上)

private async void AboutButton_Click(object sender, RoutedEventArgs e)
{
    StackPanel aboutPanel = new StackPanel();
    aboutPanel.Children.Add(
        new HyperlinkButton
        {
            Content = "View GitHub repository",
            NavigateUri = new Uri("https://www.google.com/")
        });
    aboutPanel.Children.Add(
        new HyperlinkButton
        {
            Content = "Privacy Policy",
            NavigateUri = new Uri("https://www.bing.com/")
        });

    var dlg = new ContentDialog
    {
        Title = "About",
        Content = aboutPanel,
        PrimaryButtonText = "Report a bug",
        DefaultButton = ContentDialogButton.Primary,
        CloseButtonText = "Close"
    };
    dlg.PrimaryButtonClick += ReportBug_Click;
    await dlg.ShowAsync();
}

看看下面的结果:
看下面的结果

您将第二个Hyperlink添加到错误的TextBlock 它应该是privacyPolicyTB.Inlines.Add(hyperlink2);

TextBlock privacyPolicyTB = new TextBlock();
Hyperlink hyperlink2 = new Hyperlink();
Run run2 = new Run();
run2.Text = "Privacy Policy";
hyperlink2.NavigateUri = new Uri("https://www.bing.com/");
hyperlink2.Inlines.Add(run2);
privacyPolicyTB.Inlines.Add(hyperlink2); //<-- here

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM