Friday, 15 March 2013

xaml - Can't resize Canvas size using C# -


Very easy issue: If I declare a canvas in XAML in a Windows Phone 8.1 app then:

& lt; Canvas x: name = "optioncanvas" width = "400" height = "450" ​​vertical alignment = "top" background = "white" pointermold = "optioncan pointermoid" & gt;

It works fine, but I want to adjust my app with a phone with different screen sizes. That's why I'm receiving window.Current.Bounds using the size of the screen and setting the canvas dimensions as the proportion of the total screen size in the page constructor:

  Point screenres = new point (); Double scalefactor = display. GetForCurrentView () RawPixelsPerViewPixel; ScreenRes.X = Window. Present. Bounds. Wide * Scale Factor; ScreenRes.Y = Window Present. Bounce High * scale factor; MyCanvas.Width = ScreenRes.X * 0.5; MyCanvas.Height = ScreenRes.Y * 0.7;  

But it does not seem to work. When I run the code above, check the MyCanvas.ActualHyight and MyCanvas.ActualWidth properties, both dimensions are zero and as a result, nothing shows in my canvas.

I hope you know that canvas does not clip your kids Even if its size is 0 x 0, all the controls which are inside are visible. I suggest that you use some different controls to show the background, such as border is more flexible, and leave the canvas, as (with the default 0 x 0 size) . Your code might look like this:

  & lt; Range x: name = "optionsborder" width = "400" height = "450" ​​vertical alignment = "top" background = "white" pointer mode = "optionsCanvas_PointerMoved" & gt; & Lt; Canvas x: name = "optioncanvas" vertical alignment = "top" horizontal align = "left" /> & Lt; / Border & gt;  

So you can now resize the OptionsBorder instead and do not care about the canvas. Canvas has only one advantage - you can canvas.lift and canvas. Like the Tap , you can arrange your control using X and Y.


No comments:

Post a Comment