调试后,C# WPF 与 IDE 的大小不同? [英] C# WPF different size from IDE after debugging?

查看:23
本文介绍了调试后,C# WPF 与 IDE 的大小不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Blend/VS2012 IDEDebugging 中遇到不同外观的问题.在IDE中,Rectangle是居中的,但是在编译或者调试的时候,边距的大小是不一样的.在我看来,发生这种情况是因为窗口边框的大小不同.我真的很想解决这个问题.有什么建议吗?

I am having a problem with different appearance in Blend/VS2012 IDE and in Debugging. In IDE, the Rectangle is at the center, but when it is compiled or debugged, the size of the margin is different. In my view, this occurs because the size of the window border is different. I really want to fix this problem. Any suggestions?

谢谢.

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>

结果是一样的.这是我电脑的问题吗?

Result is the same. Is this problem of my PC?

推荐答案

您的问题是窗口大小包括 Chrome 大小(窗口边框和关闭/最大最小按钮之类的东西).

Your Problem here is Window Size includes the Chrome Size(Window Border and stuff like close/max min buttons).

因此您的网格是您要求的尺寸,但它不适合您要求的窗口尺寸.

Your Grid is hence the size you requested but it does not fit in the window size you've requested.

我可以解决您的问题,使输出窗口看起来像这样,窗口大小:

I could fix your issue to make the output window look like this, with Window size:

宽度:202高度:210

Width: 202 Height: 210

在 Windows 8 上

on Windows 8

但是,您应该将窗口大小设置为 SizeToContent="WidthAndHeight"

However you should rather have your window size set to 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 不同.

Your thus not assuming Chrome sizes which might be different from different versions of Windows.

您还应该获得 Snoop.它可以帮助您非常轻松地调试此类问题,当您使用 Shift+Ctrl 将鼠标悬停在控件上时,它会在控件上显示红色边框,因此您实际上可以看到 Grid 在显示实际窗口的同时超出可见窗口UI 的元素布局

You should also get Snoop. It helps you debug such issues very easily, It shows a red border on a control when you hover on it with Shift+Ctrl, so you can actually see the Grid extend past the visible Window while showing the actual Element Layout of your UI

这篇关于调试后,C# WPF 与 IDE 的大小不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆