WindowsPhon8 ScrollViewer+计时器 实现信息自动滚动播放
XAML示例代码如下:
<!--应用程序资源-->
<Application.Resources>
<local:LocalizedStrings xmlns:local="clr-namespace:WeatherApp" x:Key="LocalizedStrings"/>
<!-- 文本样式 -->
<Style x:Key="spTwoDaysCss_txt" TargetType="TextBlock">
<Setter Property="FontSize" Value="25" />
<Setter Property="Foreground" Value="White"/>
<Setter Property="TextWrapping" Value="Wrap" />
</Style>
</Application.Resources>
<ScrollViewer Name="svToDayInfo" HorizontalScrollBarVisibility="Hidden"
VerticalScrollBarVisibility="Disabled"><TextBlock Foreground="Orange" Opacity="0.85"
Name="tbkToDayInfo" ManipulationStarted="tbkToDayInfo_ManipulationStarted"
Text="" Style="{StaticResource spTwoDaysCss_txt}" />
</ScrollViewer>
CS后台代码如下:
#region 页面级全局变量
///
/// App _currentApp
///
App _currentApp = (App)Application.Current;
///
/// 计时器对象
///
DispatcherTimer disTimer = new DispatcherTimer();
#endregion
///
/// 页面加载
///
///
///
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
//开始当前天气信息滚动播报
disTimer.Interval = TimeSpan.FromSeconds(0.01);
disTimer.Tick += disTimer_Tick;
disTimer.Start();
}
///
/// 计时器事件
///
///
///
void disTimer_Tick(object sender, EventArgs e)
{
ScrollViewInfo();
}
#region 今日天气滚动显示
/// <summary>
/// 今日天气滚动显示
/// </summary>
private void ScrollViewInfo()
{
ScrollViewer sv = this.svToDayInfo;
if (sv != null && !String.IsNullOrEmpty(this.tbkToDayInfo.Text.Trim()))
{
_currentApp.scrollOffset += 0.65;
double offset = _currentApp.scrollOffset;
if (offset > sv.ScrollableWidth)
_currentApp.scrollOffset = 0.0;
sv.ScrollToHorizontalOffset(offset);
}
}
/// <summary>
/// 停止/继续滚动
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tbkToDayInfo_ManipulationStarted(object sender, System.Windows.Input.ManipulationStartedEventArgs e)
{
if (disTimer != null)
{
if (disTimer.IsEnabled) disTimer.Stop();
else disTimer.Start();
}
e.Complete();
e.Handled = true;
}
#endregion