Find canvas size

How can I find out the size Canvas

generated in the xaml file?

For example, I am creating a Canvas

<Page
    x:Class="MyApp.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:MyApp"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

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

      

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.

+3


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.



+6


source


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

.

0


source







All Articles