简体   繁体   English

无法使用C#调整画布大小

[英]Can't resize Canvas size using C#

Very simple issue: if I declare a canvas in XAML in a Windows Phone 8.1 app like so: 非常简单的问题:如果我在Windows Phone 8.1应用程序的XAML中声明画布,如下所示:

<Canvas x:Name="OptionsCanvas" Width="400" Height="450"  VerticalAlignment="Top" Background="White" PointerMoved="OptionsCanvas_PointerMoved">

It works fine, but I want to make my app adjust to phones with different screen sizes. 效果很好,但是我想让我的应用适应屏幕尺寸不同的手机。 So I'm getting the screen size using Window.Current.Bounds and setting the canvas dimensions as a ratio of the total screen size in c# in the page's constructor like so: 所以我使用Window.Current.Bounds获取屏幕尺寸,并将画布尺寸设置为页面构造函数中c#中总屏幕尺寸的比率,如下所示:

Point screenRes = new Point();
double scaleFactor = DisplayInformation.GetForCurrentView().RawPixelsPerViewPixel;
ScreenRes.X = Window.Current.Bounds.Width * scaleFactor;
ScreenRes.Y = Window.Current.Bounds.Height * scaleFactor;
MyCanvas.Width = ScreenRes.X * 0.5;
MyCanvas.Height = ScreenRes.Y * 0.7;

But this doesn't seem to be working. 但这似乎不起作用。 When I check the MyCanvas.ActualHeight and MyCanvas.ActualWidth properties after running the above code, both dimensions are zero and consequently, nothing shows in my canvas. 在运行上述代码后,当我检查MyCanvas.ActualHeightMyCanvas.ActualWidth属性时,两个尺寸均为零,因此,画布中没有任何显示。

I hope you know that Canvas doesn't clip its children. 希望您知道Canvas不会修剪其子级。 So even if its size is 0 x 0 all controls that are inside will be visible. 因此,即使其大小为0 x 0,内部的所有控件也将可见。 I suggest you to use some different control to show the background that is more flexible, like Border and leave canvas as it is (with default 0 x 0 size). 我建议您使用其他控件来显示更灵活的背景,例如“ Border并保持画布原样(默认大小为0 x 0)。 So your code could look like this: 因此您的代码可能如下所示:

<Border x:Name="OptionsBorder" 
        Width="400" 
        Height="450"  
        VerticalAlignment="Top" 
        Background="White" 
        PointerMoved="OptionsCanvas_PointerMoved">
    <Canvas x:Name="OptionsCanvas"
            VerticalAlignment="Top"
            HorizontalAlignment="Left" />
</Border>

So now you can change the size of OptionsBorder instead and don't care about canvas. 因此,现在您可以更改OptionsBorder的大小,而不用担心画布。 Canvas has only one advantage - you can arrange your controls using Canvas.Left and Canvas.Top like X and Y. Canvas仅具有一个优点-您可以使用Canvas.LeftCanvas.Top来排列控件,例如X和Y。

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

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