为什么 UseLayoutRounding 似乎不适用于 Viewbox?
我正在尝试在 WPF 4 中编写一个基于图块的游戏。我希望游戏板能够缩放以适合窗口,因此我使用了 Viewbox;但我希望每个图块都位于漂亮、清晰的像素边界上。我可能是错的,但我的理解是,这就是新的 UseLayoutRounding 属性的用途 - 但它没有按我预期的方式工作。
这是一个演示该问题的窗口:
<Window x:Class="Tyler.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="600" Height="400" Background="Black">
<Viewbox>
<Canvas Width="1000" Height="1000">
<Rectangle Canvas.Left="100" Canvas.Top="100"
Width="100" Height="100" Fill="Gray"/>
<Rectangle Canvas.Left="200" Canvas.Top="100"
Width="100" Height="100" Fill="Gray"/>
</Canvas>
</Viewbox>
</Window>
两个矩形相邻,但由于子像素坐标(由 Viewbox 的缩放产生),我最终在它们之间出现了较深的灰色接缝。这就是我想要摆脱的——我希望它们能够无缝地融合在一起。
但UseLayoutRounding似乎没有这个效果。我尝试过在窗口、视图框、画布、矩形上设置 UseLayoutRounding="True"
——我什至尝试过一次将其放在所有这些上。对接缝没有影响。
我错过了什么(或误解)?如何使布局舍入与 Viewbox 一起使用?
I'm trying to write a tile-based game in WPF 4. I want the game board to scale to fit the window, so I'm using a Viewbox; but I want each tile to be on a nice, crisp pixel boundary. I might be wrong, but my understanding is that this is what the new UseLayoutRounding property is supposed to be for -- but it isn't working the way I expect.
Here's a window that demonstrates the issue:
<Window x:Class="Tyler.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="600" Height="400" Background="Black">
<Viewbox>
<Canvas Width="1000" Height="1000">
<Rectangle Canvas.Left="100" Canvas.Top="100"
Width="100" Height="100" Fill="Gray"/>
<Rectangle Canvas.Left="200" Canvas.Top="100"
Width="100" Height="100" Fill="Gray"/>
</Canvas>
</Viewbox>
</Window>
The two rectangles are adjacent, but because of the sub-pixel coordinates (resulting from the Viewbox's scaling), I end up with a darker gray seam between them. That's what I'm trying to get rid of -- I want them to blend together seamlessly.
But UseLayoutRounding doesn't seem to have this effect. I've tried setting UseLayoutRounding="True"
on the Window, the Viewbox, the Canvas, the Rectangles -- I've even tried putting it on all of them at once. There's no effect on the seam.
What am I missing (or misunderstanding)? How can I get layout rounding to work with a Viewbox?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
SnapsToDevicePixels=True
就是你想要的SnapsToDevicePixels=True
is what you want