Wp8滚动区域(ScrollViewer)控件的使用
1. <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
<TextBlock Text="我的应用程序" Style="{StaticResource PhoneTextNormalStyle}" Margin="12,0"/>
<TextBlock Text="Grid控件" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel>
<ScrollViewer x:Name="ScrollViewer1" HorizontalAlignment="Left" Height="607" Grid.Row="1" VerticalAlignment="Top" Width="336" Background="#FFC9C1C1" VerticalScrollBarVisibility="Hidden" HorizontalScrollBarVisibility="Visible">
<StackPanel x:Name="stkpnlImage" Width="324"/> <!--不要设置stkpnlImage的高度,否则不能滚动-->
</ScrollViewer>
<Button x:Name="Top" Content="上划" HorizontalAlignment="Left" Height="157" Margin="326,149,-13,0" VerticalAlignment="Top" Width="167" Grid.RowSpan="2"/>
<Button x:Name="Dowm" Content="下划" HorizontalAlignment="Left" Height="157" Margin="326,119,-13,0" VerticalAlignment="Top" Width="167" Grid.Row="1"/>
<Button x:Name="Stop" Content="停止" HorizontalAlignment="Left" Height="157" Margin="326,252,-13,0" VerticalAlignment="Top" Width="167" Grid.Row="1"/>
2.
namespace PhoneApp1 { public partial class MainPage : PhoneApplicationPage { private DispatcherTimer trmDown; private DispatcherTimer trmUp; // 构造函数 public MainPage() { InitializeComponent(); for (int i = 0; i < 30; i++) { Image image = new Image(); image.Width =image.Height=250; if (i % 4 == 0) { image.Source = new BitmapImage(new Uri("/Image/1.jpg", UriKind.Relative)); } else if (i % 4 == 1) { image.Source = new BitmapImage(new Uri("../Image/2.jpg", UriKind.Relative)); } else if (i % 4 == 2) { image.Source = new BitmapImage(new Uri("../Image/3.jpg", UriKind.Relative)); } else { image.Source = new BitmapImage(new Uri("/Image/4.jpg", UriKind.Relative)); } stkpnlImage.Children.Add(image); } trmDown = new DispatcherTimer(); trmUp = new DispatcherTimer(); trmDown.Interval = TimeSpan.FromMilliseconds(500); trmUp.Interval = TimeSpan.FromMilliseconds(500); trmDown.Tick += trmDown_Tick; trmUp.Tick += trmUp_Tick; Top.Click += Top_Click; Dowm.Click += Dowm_Click; Stop.Click += Stop_Click; } void Stop_Click(object sender, RoutedEventArgs e) { trmDown.Stop(); trmUp.Stop(); } void Dowm_Click(object sender, RoutedEventArgs e) { trmUp.Stop(); trmDown.Start(); } void Top_Click(object sender, RoutedEventArgs e) { trmDown.Stop(); trmUp.Start(); } void trmUp_Tick(object sender, EventArgs e) { ScrollViewer1.ScrollToVerticalOffset(ScrollViewer1.VerticalOffset - 10); } void trmDown_Tick(object sender, EventArgs e) { ScrollViewer1.ScrollToVerticalOffset(ScrollViewer1.VerticalOffset +10); } } }