如何解决为什么堆栈面板会忽略“方向”属性?
我想在ContentDialog中使用带有两个具有两个uri的文本块的堆栈面板。问题是,尽管我将该属性设置为Vertical,但它没有任何效果,这是视觉效果
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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。