我们有一个绝对布局的要求,允许滚动它的内容(子控件)。布局和所有的Child控件都是在应用程序运行期间随时创建和添加的;因此,XAML不适用于创建布局。WP8画布内滚动查看器 - 必须代码隐藏
不幸的是,使用后面的代码很难实现所需的滚动行为;与XAML不同,我们可以非常简单地实现它。
下面的XAML演示所需的行为
<phone:PhoneApplicationPage
x:Class="TestWP8App.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
shell:SystemTray.IsVisible="True">
<ScrollViewer HorizontalAlignment="Left" VerticalAlignment="Top" Width="480">
<Canvas Height="1500">
<Button Content="1" Height="300" Canvas.Left="10" Canvas.Top="10" Width="120"/>
<Button Content="2" Height="300" Canvas.Left="10" Canvas.Top="110" Width="120"/>
<Button Content="3" Height="300" Canvas.Left="10" Canvas.Top="210" Width="120"/>
<Button Content="4" Height="300" Canvas.Left="10" Canvas.Top="310" Width="120"/>
<Button Content="5" Height="300" Canvas.Left="10" Canvas.Top="410" Width="120"/>
</Canvas>
</ScrollViewer>
</phone:PhoneApplicationPage>
通过上述特性转换为C#代码,这是可能达到相同的“看”,只有内容从未滚动。
有谁知道如何通过代码隐藏实现相同的结果吗?或者知道一个例子的位置,完全是这样使用代码隐藏的吗?
感谢您的答复,但有些事必须是不同的,它是仿佛ScrollPanel已经成长为配合画布的大小,因此一直推关闭屏幕。如果我玩的尺寸,我也可以看到按钮'溢出'容器的边界,当我希望ScrollViewer'剪辑'它的可视窗口外的任何子控件。关于ScrollViewer设置高度/宽度的规则是什么?它应该留给自己的大小? – Gov