我們有一個絕對佈局的要求,允許滾動它的內容(子控件)。佈局和所有的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