运行时在WinRT应用程序中更改UserControl布局



我有一个UserControl,它用于WinRT应用程序中的Popup。它在设计器中看起来很棒,但当我运行它时,布局会发生变化,UserControl的子控件会以不需要的方式调整大小:A)它在垂直方向上比我在设计时设置的以及我为承载它的弹出窗口明确设置的要小。B)如果主文本消息很长,它会将整个弹出窗口扩展为与文本字符串一样长,而不是将其包装以满足TextBlock的固定宽度。如果文本消息很短,弹出窗口似乎会移动到死点的左侧。

注意,UserControl中有三个子网格。任何时候都只放映其中一个,所以据我所知,这不是他们之间的冲突。当弹出窗口第一次运行时,我显示网格问题网格,并隐藏gridRateAndReviewgridSendFeedback栅格。当在网格问题网格上单击其中一个按钮时,我会隐藏该网格,并根据单击的按钮显示其他子网格之一当我隐藏一个元素时,我将其opacity属性设置为0,将IsHitTestVisible特性设置为false;将Visibility设为Collapsed。当我显示一个元素时,我会执行相反的操作。

1) 为什么子控件在运行时调整大小?

2) 为什么主文本消息不换行,以及如何阻止它调整包含它的TextBlock的大小?

注意,我已经为每个容器子控件尝试了"拉伸"one_answers"居中"的各种组合。似乎什么都不管用。这是用户控件的XAML:

<UserControl x:Name="userControl"
x:Class="RecNote.UserControls.RateMyApp"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:RecNote.UserControls"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400" 
DataContext="{Binding Main, Mode=OneWay, Source={StaticResource Locator}}" HorizontalAlignment="Center" VerticalAlignment="Center">
<Border CornerRadius="6" BorderThickness="2">
<Grid x:Name="gridOuter">
<Grid.RowDefinitions>
<RowDefinition Height="31*"/>
<RowDefinition Height="117*"/>
</Grid.RowDefinitions>
<TextBlock x:Name="lblTitle" Grid.Row="0" HorizontalAlignment="Center" TextWrapping="Wrap" Text="Feedback Please" VerticalAlignment="Center" FontSize="24" Height="29" Width="195" FontFamily="Bookman Old Style" Foreground="#FFF5A3A3"/>
<!-- The contents first shown to the user and ask them if they're happy with the product. -->
<Grid x:Name="gridQuestion" Grid.Row="1" d:IsHidden="True">
<Grid.RowDefinitions>
<RowDefinition Height="137*"/>
<RowDefinition Height="35*"/>
<RowDefinition Height="84*"/>
</Grid.RowDefinitions>
<TextBlock TextWrapping="Wrap" Text="{Binding RatingMessage, ElementName=userControl}" FontSize="24" Grid.Row="0" Margin="55,25,54,25"/>
<TextBlock TextWrapping="Wrap" FontSize="24" Grid.Row="1" Text="Are you enjoying RecNote?" HorizontalAlignment="Center" VerticalAlignment="Center" Height="29" Margin="55,5,54,1" Width="287"/>
<StackPanel x:Name="stackButtons" Grid.Row="2" Orientation="Horizontal" >
<Button x:Name="btnYes" Content="YES" VerticalAlignment="Center" Width="150" Margin="35,0,30,0" Background="#FF464F41" Click="btnYes_Click" />
<Button x:Name="btnNo" Content="NO" VerticalAlignment="Center" Width="150" Background="#FF875F4D" Click="btnNo_Click"/>
</StackPanel>
</Grid>
<!-- The contents shown to the user if they say YES that prompts them to rate/review the app. -->
<Grid x:Name="gridRateAndReview" Grid.Row="1" d:IsHidden="True" >
<Grid.RowDefinitions>
<RowDefinition Height="183*"/>
<RowDefinition Height="73*"/>
</Grid.RowDefinitions>
<TextBlock TextWrapping="Wrap" Margin="25" FontSize="22" Text="{Binding GladYouAreEnjoyingAppMessage, ElementName=userControl}"/>
<Button x:Name="btnRateAndReview" Content="Rate &amp; Review" VerticalAlignment="Center" Width="150" Background="#FF464F41" Grid.Row="1" HorizontalAlignment="Center" Click="btnRateAndReview_Click" />
</Grid>
<!-- The contents shown to the user if they say NO that prompts them to rate/review the app. -->
<Grid x:Name="gridSendFeedback" Grid.Row="1" d:IsHidden="True" >
<Grid.RowDefinitions>
<RowDefinition Height="183*"/>
<RowDefinition Height="73*"/>
</Grid.RowDefinitions>
<TextBlock TextWrapping="Wrap" Margin="25" FontSize="22" Text="{Binding SendFeedbackMessage, ElementName=userControl}"/>
<Button x:Name="btnSendFeedback" Content="Send Feedback" VerticalAlignment="Center" Width="150"  Background="#FF875F4D" Grid.Row="1" HorizontalAlignment="Center" Click="btnSendFeedback_Click" />
</Grid>
</Grid>
</Border>
</UserControl>

这是在弹出窗口上显示用户控件的代码:

public static Popup ShowPopup(UserControl userControl, int width, int height)
{
if (userControl == null)
throw new ArgumentNullException("The user control is unassigned.");
if (width <= 0)
throw new ArgumentException("The width is zero or negative.");
if (height <= 0)
throw new ArgumentException("The height is zero or negative.");
// Create the popup.
Popup popup = new Popup();
// --------------- POPUP STYLING -----------------------
// Set the width and height.
popup.Width = width;
popup.Height = height;
// Center the popup on the screen.
// popup.HorizontalAlignment = HorizontalAlignment.Center;
// popup.VerticalAlignment = VerticalAlignment.Center;
// Center the popup.
popup.HorizontalOffset = (Window.Current.Bounds.Width - popup.Width) / 2;
popup.VerticalOffset = (Window.Current.Bounds.Height - popup.Height) / 2;
popup.MinWidth = width;
popup.MaxWidth = width;
popup.MinHeight = height;
popup.MaxHeight = height;
popup.IsLightDismissEnabled = true;
// Make the user control a child of the popup.
popup.Child = userControl;
// Show it.
popup.IsOpen = true;
return popup;
}

它们会重新调整大小,因为您的*声明也允许它们提供任何可用空间(当然,减去您有固定大小的元素。d:集合大小仅限设计器使用。)至于缺少包装,没有父元素提供调用它的限制(除了在您有一个硬集Width的实例上,我认为这个实例是包装的),这也是基于您的*使用情况。

至于隐藏它们时,不需要显式地分别设置OpacityHitTestVisibilityVisibility。只要切换Visibility,你就完成了。

希望这能有所帮助。

是否可以在Window中检查值是否正确检索。现在的边界。宽度或窗口。现在的边界。身高try TextWrapping="Wholeworks">

最新更新