运行时项目周围不必要的空白区域

时间:2017-03-23 14:36:36

标签: c# uwp

我正在开发一个小型的单页c#UWP应用程序。当我在设计视图中看到它时,一切看起来都没问题,没有不必要的空白区域。但是当我运行它时,窗口在整个设计周围有一个很大的空白边框,看起来很糟糕。下面我已经包含了设计截图和运行窗口。如何删除多余的空格?

设计视图(没有奇怪的空白区域):

Design view, no weird white space

跑步(奇怪的空白区域):

Running, weird white space

应用程序窗口相当小,400x200。我尝试过更改这些值,但它只是将控件挤压在一起,并在边框周围添加更多的空白区域。

编辑:显然,尽管设计视图很好,但我的XAML存在问题,所以你走了。

<Page x:Class="ReservationManager.Views.StartPage"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:local="using:ReservationManager.Views"
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
      mc:Ignorable="d"
      DataContext="{Binding StartPageInstance, Source={StaticResource Locator}}"
      Width="400"
      Height="200">

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition />
            <RowDefinition />
        </Grid.RowDefinitions>

        <Grid Grid.Row="0">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
            <TextBlock Name="Label1"
                       VerticalAlignment="Center"
                       HorizontalAlignment="Center"
                       Text="Label 1: "
                       FontFamily="Segoe UI Historic" />
            <TextBox Grid.Column="1"
                     VerticalAlignment="Center"
                     Name="TextBox1"
                     MaxLength="7"
                     Text="{Binding tb1, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
        </Grid>

        <Grid Grid.Row="0"
              Grid.Column="1">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
            <TextBlock Grid.Column="0"
                       Name="Label2"
                       VerticalAlignment="Center"
                       HorizontalAlignment="Center"
                       Text="Label 2: "
                       Margin="10,0,0,0"
                       FontFamily="Segoe UI Historic" />
            <TextBox Grid.Column="1"
                     VerticalAlignment="Center"
                     Name="TextBox2"
                     Text="{Binding tb2, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
        </Grid>

        <Grid Grid.Row="1"
              Grid.Column="0">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
            <TextBlock Grid.Column="0"
                       Name="Label3"
                       VerticalAlignment="Center"
                       HorizontalAlignment="Center"
                       Text="Label 3: "
                       FontFamily="Segoe UI Historic" />
            <ComboBox Grid.Column="1"
                      VerticalAlignment="Center"
                      Name="Dropdown"
                      ItemsSource="{Binding cb}"
                      SelectedValue="{Binding cb1, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                      DisplayMemberPath="Name" />
        </Grid>

        <StackPanel VerticalAlignment="Center"
                    HorizontalAlignment="Center"
                    Grid.Row="1"
                    Grid.Column="1">
            <RadioButton IsChecked="{Binding IsReserveChecked, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                         GroupName="Radio"
                         Content="radio1" />
            <RadioButton IsChecked="{Binding IsUnreserveChecked, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                         GroupName="Radio"
                         Content="radio2" />
        </StackPanel>

        <TextBlock Grid.Row="2"
                   Grid.Column="0"
                   VerticalAlignment="Center"
                   HorizontalAlignment="Center"
                   Foreground="Red">
            Warning: ----
        </TextBlock>

        <Button Grid.Row="2"
                Grid.Column="1"
                HorizontalAlignment="Center"
                VerticalAlignment="Center"
                Content="Execute"
                Command="{Binding ExecuteCommand}" />
    </Grid>
</Page>

1 个答案:

答案 0 :(得分:1)

问题是由于“首选最小尺寸”默认设置得太高。

将以下行添加到后面的代码中修复了问题:

ApplicationView.GetForCurrentView().SetPreferredMinSize(new Size { Width = 250, Height = 200 });