C# WPF调试后与IDE中大小不同?

5

http://i.stack.imgur.com/2gOLN.png

我在Blend/VS2012 IDEDebugging中遇到了外观不同的问题。 在IDE中,Rectangle处于中心位置,但是编译或调试时,边缘大小不同。我认为这是因为窗口边框大小不同所致。我真的想解决这个问题。有什么建议吗?

谢谢。

XAML

<Window x:Class="WpfApplication2.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="202" Width="194">
<Grid HorizontalAlignment="Left" Height="171" VerticalAlignment="Top" Width="186">
    <Rectangle Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="151" Margin="10,10,0,0" Stroke="Black" VerticalAlignment="Top" Width="166"/>
</Grid>

编辑后的 XAML 代码:

<Window x:Class="WpfApplication2.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="202" Width="194">
<Grid HorizontalAlignment="Left" Height="171" VerticalAlignment="Top" Width="186">
    <Rectangle Fill="#FFF4F4F5" HorizontalAlignment="Center" Height="151" Margin="10" Stroke="Black" VerticalAlignment="Center" Width="166"/>
</Grid>

结果是一样的。这是我的电脑出了问题吗?

你能提供XAML代码吗? - Xelom
Xelom,我已经添加了XAML代码。感谢您的关注。 - jn4kim
将边距更改为Margin="10",并将水平和垂直对齐设置为Stretch或Center。 - Pavel Voronin
voroninp,我已经编辑了源代码,但是结果仍然相同。 - jn4kim
1个回答

5
你的问题在于窗口大小包括Chrome大小(窗口边框和关闭/最大化/最小化按钮等)。
因此,你的网格是你请求的大小,但它不适合你请求的窗口大小。
我可以解决你的问题,使输出窗口看起来像这样,具有窗口大小:
宽度:202 高度:210
在Windows 8上。
然而,你应该将窗口大小设置为SizeToContent =“WidthAndHeight” 例如:
<Window x:Class="WpfApplication4.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication4"
        x:Name="window"
        Title="MainWindow"
        SizeToContent="WidthAndHeight">
<Grid HorizontalAlignment="Left" Height="171" VerticalAlignment="Top" Width="186">
    <Rectangle Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="151" Margin="10,10,0,0" Stroke="Black" VerticalAlignment="Top" Width="166"/>
</Grid>
</Window>

你因此不应假设Chrome的大小与Windows的不同版本可能不同。
你还应该获取Snoop。它可以帮助您轻松调试此类问题,当您使用Shift + Ctrl悬停在控件上时,它会显示红色边框,因此您实际上可以看到Grid超出可见窗口而显示UI的实际元素布局。

Image


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接