UWP组在运行时有奇怪的行为

时间:2019-06-03 14:16:11

标签: c# xaml uwp-xaml

我不熟悉XAML for UWP。因此,我尝试使用.XAML制作4个分组框。当我尝试运行代码时,它会拉伸组框,使GUI看起来很糟糕。 .XAML中的哪些内容可能会导致类似这种情况?

在设计期间,它看起来像:

Design Time

当我在模拟器中运行代码时,它看起来像:

Run time GUI

.XAML代码:

<Grid>
  <ComboBox HorizontalAlignment="Center" Margin="0,48,0,0" VerticalAlignment="Top" Height="42" Width="232"/>
  <ListView x:Name="DatabaseInfo" Header="Database Info." BorderThickness="2" BorderBrush="Black" SelectionMode="None" Margin="212,130,551,276" FontFamily="Tahoma" >
     <ListView.HeaderTemplate>
        <DataTemplate>
           <ListViewHeaderItem Content="{Binding}" Height="40" />
        </DataTemplate>
     </ListView.HeaderTemplate>

     <StackPanel Width="230" Height="30" Orientation="Horizontal" >
        <TextBlock Name="ServerNameDisplay" HorizontalAlignment="Left" Text="Server" Width="115" Height="25" />
        <TextBox Name="Server" HorizontalAlignment="Right" Text="" Height="25" Width="115" />
     </StackPanel>

     <StackPanel Width="230" Height="30" Orientation="Horizontal" >
        <TextBlock Name="DatabaseNameDisplay" HorizontalAlignment="Left" Text="Database" Width="115" Height="25" />
        <TextBox Name="Database" HorizontalAlignment="Right" Text="" Height="25" Width="115"/>
     </StackPanel>
     <StackPanel Width="230" Height="30" Orientation="Horizontal" >
        <TextBlock Name="UserNameDisplay" HorizontalAlignment="Left" Text="UserName" Width="115" Height="25" />
        <TextBox Name="UserName" HorizontalAlignment="Right" Text="" Height="25" Width="115"/>
     </StackPanel>
     <StackPanel Width="230" Height="30" Orientation="Horizontal" >
        <TextBlock Name="PasswordDisplay" HorizontalAlignment="Left" Text="Password" Width="115" Height="25" />
        <TextBox Name="Password" HorizontalAlignment="Right" Text="" Height="25" Width="115"/>
     </StackPanel>
  </ListView>
  <ListView x:Name="RabbitMQInfo" Header="Rabbit MQ Info." BorderThickness="2" BorderBrush="Black" SelectionMode="None" Margin="564,130,199,350" FontFamily="Tahoma" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" >
     <ListView.HeaderTemplate>
        <DataTemplate>
           <ListViewHeaderItem Content="{Binding}" Height="40" />
        </DataTemplate>
     </ListView.HeaderTemplate>
     <StackPanel Width="230" Height="30" Orientation="Horizontal" >
        <TextBlock Name="RabbitMQUserDisplay" HorizontalAlignment="Left" Text="UserName" Width="115" Height="25" />
        <TextBox Name="RabbitMQUser" HorizontalAlignment="Right" Text="" Height="25" Width="115"/>
     </StackPanel>
     <StackPanel Width="230" Height="30" Orientation="Horizontal" >
        <TextBlock Name="RabbitMQPassDisplay" HorizontalAlignment="Left" Text="Password" Width="115" Height="25" />
        <TextBox Name="RabbitMQPass" HorizontalAlignment="Right" Text="" Height="25" Width="115"/>
     </StackPanel>
  </ListView>
  <ListView x:Name="MachineInfo" Header="Name Info." BorderThickness="2" BorderBrush="Black" SelectionMode="None" Margin="212,398,551,125" FontFamily="Tahoma" >
     <ListView.HeaderTemplate>
        <DataTemplate>
           <ListViewHeaderItem Content="{Binding}" Height="40" />
        </DataTemplate>
     </ListView.HeaderTemplate>
     <StackPanel Width="230" Height="30" Orientation="Horizontal" >
        <TextBlock Name="NameDisplay" HorizontalAlignment="Left" Text="Name" Width="115" Height="25" />
        <TextBox Name="Name" HorizontalAlignment="Right" Text="" Height="25" Width="115"/>
     </StackPanel>
  </ListView>
  <ListView x:Name="IPAddressInfo" Header="IP Address Info." BorderThickness="2" BorderBrush="Black" SelectionMode="None" Margin="564,332,199,156" FontFamily="Tahoma" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" >
     <ListView.HeaderTemplate>
        <DataTemplate>
           <ListViewHeaderItem Content="{Binding}" Height="40" />
        </DataTemplate>
     </ListView.HeaderTemplate>
     <StackPanel Width="230" Height="30" Orientation="Horizontal" >
        <TextBlock Name="IPAddressDisplay" HorizontalAlignment="Left" Text="IPAddress" Width="115" Height="25" />
        <TextBox Name="IPAddress" HorizontalAlignment="Right" Text="" Height="25" Width="115"/>
     </StackPanel>
     <StackPanel Width="230" Height="30" Orientation="Horizontal">
        <TextBlock Name="PortDisplay" HorizontalAlignment="Left" Text="Port" Width="115" Height="25" />
        <TextBox Name="Port" HorizontalAlignment="Right" Text="" Height="25" Width="115"/>
     </StackPanel>
  </ListView>
  <Button x:Name="btnSave" Content="Save" Click="btnSave_Click"  HorizontalAlignment="Center" Margin="0,539,0,0" VerticalAlignment="Top" Width="100" Height="50"/>

关于UWP应用程序的另一个问题是,您是否必须运行运行在Bin文件夹中的.exe的特殊文件?尝试以Admin身份运行.exe时,它只会崩溃并带有未处理的Win32异常。

1 个答案:

答案 0 :(得分:0)

  

当我尝试运行代码时,它拉伸了组合框,使GUI看起来很糟糕。 .XAML中的哪些内容可能导致这种情况?

您使用了Margin属性来控制XAML控件的位置,这将导致您看到的混乱的布局。

请参阅Layout文档,以了解如何在UWP中使用XAML进行良好的布局。

例如,您可以简单地使用Grid控件将这些控件放在不同的行和列上。

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition></ColumnDefinition>
        <ColumnDefinition></ColumnDefinition>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="*"></RowDefinition>
        <RowDefinition Height="4*"></RowDefinition>
        <RowDefinition Height="4*"></RowDefinition>
        <RowDefinition Height="*"></RowDefinition>
    </Grid.RowDefinitions>
    <ComboBox HorizontalAlignment="Center" Grid.Row="0" Grid.ColumnSpan="2" VerticalAlignment="Center" Height="42" Width="232"/>
    <ListView x:Name="DatabaseInfo" Grid.Row="1" Grid.Column="0" Header="Database Info." BorderThickness="2" BorderBrush="Black" SelectionMode="None" FontFamily="Tahoma" >
        <ListView.HeaderTemplate>
            <DataTemplate>
                <ListViewHeaderItem Content="{Binding}" Height="40" />
            </DataTemplate>
        </ListView.HeaderTemplate>

        <StackPanel Width="230" Height="30" Orientation="Horizontal" >
            <TextBlock Name="ServerNameDisplay" HorizontalAlignment="Left" Text="Server" Width="115" Height="25" />
            <TextBox Name="Server" HorizontalAlignment="Right" Text="" Height="25" Width="115" />
        </StackPanel>

        <StackPanel Width="230" Height="30" Orientation="Horizontal" >
            <TextBlock Name="DatabaseNameDisplay" HorizontalAlignment="Left" Text="Database" Width="115" Height="25" />
            <TextBox Name="Database" HorizontalAlignment="Right" Text="" Height="25" Width="115"/>
        </StackPanel>
        <StackPanel Width="230" Height="30" Orientation="Horizontal" >
            <TextBlock Name="UserNameDisplay" HorizontalAlignment="Left" Text="UserName" Width="115" Height="25" />
            <TextBox Name="UserName" HorizontalAlignment="Right" Text="" Height="25" Width="115"/>
        </StackPanel>
        <StackPanel Width="230" Height="30" Orientation="Horizontal" >
            <TextBlock Name="PasswordDisplay" HorizontalAlignment="Left" Text="Password" Width="115" Height="25" />
            <TextBox Name="Password" HorizontalAlignment="Right" Text="" Height="25" Width="115"/>
        </StackPanel>
    </ListView>
    <ListView x:Name="RabbitMQInfo" Grid.Row="1" Grid.Column="1" Header="Rabbit MQ Info." BorderThickness="2" BorderBrush="Black" SelectionMode="None" FontFamily="Tahoma" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" >
        <ListView.HeaderTemplate>
            <DataTemplate>
                <ListViewHeaderItem Content="{Binding}" Height="40" />
            </DataTemplate>
        </ListView.HeaderTemplate>
        <StackPanel Width="230" Height="30" Orientation="Horizontal" >
            <TextBlock Name="RabbitMQUserDisplay" HorizontalAlignment="Left" Text="UserName" Width="115" Height="25" />
            <TextBox Name="RabbitMQUser" HorizontalAlignment="Right" Text="" Height="25" Width="115"/>
        </StackPanel>
        <StackPanel Width="230" Height="30" Orientation="Horizontal" >
            <TextBlock Name="RabbitMQPassDisplay" HorizontalAlignment="Left" Text="Password" Width="115" Height="25" />
            <TextBox Name="RabbitMQPass" HorizontalAlignment="Right" Text="" Height="25" Width="115"/>
        </StackPanel>
    </ListView>
    <ListView x:Name="MachineInfo" Grid.Row="2" Grid.Column="0" Header="Name Info." BorderThickness="2" BorderBrush="Black" SelectionMode="None" FontFamily="Tahoma" >
        <ListView.HeaderTemplate>
            <DataTemplate>
                <ListViewHeaderItem Content="{Binding}" Height="40" />
            </DataTemplate>
        </ListView.HeaderTemplate>
        <StackPanel Width="230" Height="30" Orientation="Horizontal" >
            <TextBlock Name="NameDisplay" HorizontalAlignment="Left" Text="Name" Width="115" Height="25" />
            <TextBox Name="Name" HorizontalAlignment="Right" Text="" Height="25" Width="115"/>
        </StackPanel>
    </ListView>
    <ListView x:Name="IPAddressInfo" Grid.Row="2" Grid.Column="1" Header="IP Address Info." BorderThickness="2" BorderBrush="Black" SelectionMode="None" FontFamily="Tahoma" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" >
        <ListView.HeaderTemplate>
            <DataTemplate>
                <ListViewHeaderItem Content="{Binding}" Height="40" />
            </DataTemplate>
        </ListView.HeaderTemplate>
        <StackPanel Width="230" Height="30" Orientation="Horizontal" >
            <TextBlock Name="IPAddressDisplay" HorizontalAlignment="Left" Text="IPAddress" Width="115" Height="25" />
            <TextBox Name="IPAddress" HorizontalAlignment="Right" Text="" Height="25" Width="115"/>
        </StackPanel>
        <StackPanel Width="230" Height="30" Orientation="Horizontal">
            <TextBlock Name="PortDisplay" HorizontalAlignment="Left" Text="Port" Width="115" Height="25" />
            <TextBox Name="Port" HorizontalAlignment="Right" Text="" Height="25" Width="115"/>
        </StackPanel>
    </ListView>
    <Button x:Name="btnSave" Content="Save"  Grid.Row="3" Grid.ColumnSpan="2"  HorizontalAlignment="Center"  VerticalAlignment="Center" Width="100" Height="50"/>
</Grid>
  

关于UWP应用程序的另一个问题是,您是否必须运行运行在Bin文件夹中的.exe的特殊文件?尝试以Admin身份运行.exe时,它只会崩溃并带有未处理的Win32异常。

UWP在沙箱中运行,与经典的桌面应用程序不同。您不能直接双击“ .exe”文件来启动它。在Visual Studio中进行编码时,可以按 F5 启动并调试它。如果已部署,则可以从Windows的“开始”菜单中启动它。

  

提示:请下次不要在一个帖子中发布多个问题。