首页 > 解决方案 > UWP 组在运行时有奇怪的行为

问题描述

我是 UWP 的 XAML 新手。所以我正在尝试使用 .XAML 制作 4 个组合框。当我尝试运行代码时,它会拉伸使 GUI 看起来很糟糕的组框。.XAML 中的什么可能导致这样的事情?

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

设计时间

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

运行时图形用户界面

.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?当尝试以管理员身份运行 .exe 时,它​​只会崩溃并出现未处理的 win32 异常。

标签: c#xamluwp-xaml

解决方案


当我尝试运行代码时,它会拉伸使 GUI 看起来很糟糕的组框。.XAML 中的什么可能导致这样的事情?

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

请参阅布局文档以了解如何在 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?当尝试以管理员身份运行 .exe 时,它​​只会崩溃并出现未处理的 win32 异常。

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

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


推荐阅读