简体   繁体   English

如何获得设置为“自动”的Canvas元素的宽度和高度?

[英]How do I get the width and height of a Canvas element that is set to 'Auto'?

I'm developing an App for Windows Phone 8.1, and I have a canvas that draws a ball onto it programmatically. 我正在为Windows Phone 8.1开发一个应用程序,并且我有一个画布以编程方式在其上绘制一个球。 My problem is I want to set the boundaries of the canvas to be that of its width and height, but I'm using grids with relative sizes for the size of the canvas itself. 我的问题是我想将画布的边界设置为宽度和高度的边界,但是我正在使用具有相对大小的网格作为画布本身的大小。

I'm wondering how I can get the width and height of the canvas - I've tried actualwidth and actualheight but they're just showing '0'. 我想知道如何获得画布的宽度和高度-我已经尝试过实际宽度和实际高度,但它们只是显示为'0'。

You can put the Canvas inside something (a Border, a Grid), and then get the ActualWidth and ActualHeight from the container. 您可以将Canvas放到某些东西(边框,网格)中,然后从容器中获取ActualWidthActualHeight

I have the impression that Canvas does not expand to fill container, so it is better to get dimensions from the container itself. 我的印象是Canvas无法扩展以填充容器,因此最好从容器本身获取尺寸。 Just be sure the container layout is the same you would want your Canvas to have. 只要确保容器布局与您希望画布具有的布局相同即可。

I got the width and height by implementing the SizeChanged event for the Canvas in which I call my own redraw() function. 我通过为Canvas实现SizeChanged事件来获得宽度和高度,在其中我调用了自己的redraw()函数。 MyCanvas.ActualWidth then have the correct value. MyCanvas.ActualWidth然后具有正确的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM