簡體   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