Find canvas size

How can I find out the size Canvas

generated in the xaml file?

For example, I am creating a Canvas


        <Canvas x:Name="canvas" Background="White">


and full screen is white or whatever color I specified for this canvas

than in my class i have tried but no luck

double h = canvas.Height; // NaN
h = canvas.ActualHeight;  // 0


so how do you know the actual size of this canvas? or the size is 0, but how to make the size of the fullscreen canvas?

I am a new C # and metro developer and am so confused that everything works as compared to iOS.


source to share

2 answers

Where in your code do you check the canvas size? I am assuming you are doing this in the page constructor or somewhere else, which is done before the UI layout starts. In this case, all autosized items (height or width NaN) are still sized by default. If you check the size after the layout is complete, as in the Loaded event handler on your page, you should see the actual render size.



If yours Canvas

only contains one Image

, you can use:

var Width = ((Image) (MyCanvas.Children[0])).Width;
var Height = ((Image) (MyCanvas.Children[0])).Height;


Of course, you can turn off two casts Image

for another element if yours Canvas

contains one of some of the other elements.

Alternatively, just give the wrapped Image

(or other element) a name and specify it as MyImage.Width




All Articles