簡體   English   中英

在WinRT應用程序中運行時是否更改了UserControl布局?

[英]UserControl layout changing at run-time in WinRT app?

我有一個WinRT應用程序中的彈出窗口上使用的UserControl。 在設計器中看起來很棒,但是當我運行它時,布局會發生變化,UserControl的子控件會以不需要的方式調整自身大小:A)垂直方向上的尺寸小於我在設計時設置的尺寸,也比我為承載它的彈出窗口所明確設置的尺寸。 B)如果主文本消息很長,它將整個彈出窗口擴大到與文本字符串一樣長,而不是將其包裝成TextBlock的固定寬度。 如果短信很短,則彈出窗口似乎會移到死點的左側。

請注意,UserControl中有三個子網格。 在任何時候都只顯示其中之一,據我所知這並不是它們之間的沖突。 第一次運行彈出窗口時,我顯示gridQuestion網格並隱藏gridRateAndReviewgridSendFeedback網格。 當在gridQuestion網格上單擊一個按鈕時,我將隱藏該網格並根據單擊的按鈕顯示其他子網格之一。 當我隱藏元素時,將其opacity屬性設置為0 ,將其IsHitTestVisible屬性設置為false ,將其Visibility屬性設置為Collapsed 當顯示一個元素時,我做相反的事情。

1)為什么子控件會在運行時調整自身大小?

2)為什么主文本消息沒有自動換行,如何停止調整包含它的TextBlock的大小?

注意,我已經為每個容器子控件嘗試了Stretch和Center的各種組合。 似乎沒有任何作用。 這是用戶控件的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.Current.Bounds.Width或Window.Current.Bounds.Height中正確檢索了該值。 嘗試TextWrapping =“ Wholewords”

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM