silverlight使用定时器右下角定时弹窗,模拟信息报警。
代码如下:
<Canvas VerticalAlignment="Bottom" HorizontalAlignment="Right">
<Canvas.Resources>
<Storyboard x:Name="storyPopUp">
<DoubleAnimation Duration="0:0:2" From="0" To="-200" Storyboard.TargetName="canvasPopup" Storyboard.TargetProperty="(Canvas.Top)" />
</Storyboard>
<Storyboard x:Name="storyPopDown">
<DoubleAnimation Duration="0:0:2" From="-200" To="0" Storyboard.TargetName="canvasPopup" Storyboard.TargetProperty="(Canvas.Top)" />
</Storyboard>
</Canvas.Resources>
<Canvas VerticalAlignment="Bottom" HorizontalAlignment="Right" x:Name="canvasPopup" Margin="-200 0 0 0" Width="200" Height="200">
<Border BorderThickness="1,1,1,1" Background="Azure" HorizontalAlignment="Right" VerticalAlignment="Top"
BorderBrush="Gold" Width="200" Height="200">
<Border.Effect>
<DropShadowEffect />
</Border.Effect>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="15" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<TextBlock Text="学习右下角弹窗" FontSize="12" FontWeight="Black" Foreground="Black" Grid.Row="0" />
<Border Background="White" Margin="0 5 0 0" CornerRadius="5" Grid.Row="1">
<TextBlock Text="弹窗显示内容!" Margin="3" TextWrapping="Wrap" FontSize="12" Width="190" />
</Border>
</Grid>
</Border>
</Canvas>
</Canvas>
后台代码:
System.Windows.Threading.DispatcherTimer gapTimer;
void initAlarmTimer()
{
gapTimer = new System.Windows.Threading.DispatcherTimer();
gapTimer.Interval = new TimeSpan(0, 0, 0, 10);
gapTimer.Tick += new EventHandler(gapTimer_Tick);
gapTimer.Start();
}
void gapTimer_Tick(object sender, EventArgs e)
{
popAlarm();
}
#region 报警信息
private void popAlarm()
{
storyPopUp.Begin();
alarmTitle.Text = "报警类型";
alarmContent.Text = "警情明细" + DateTime.Now.ToLongTimeString();
System.Windows.Threading.DispatcherTimer timer = new System.Windows.Threading.DispatcherTimer();
timer.Interval = new TimeSpan(0, 0, 0, 0, 4500);
timer.Tick += new EventHandler((sender2, e2) =>
{
storyPopUp.Stop();
storyPopDown.Begin();
timer.Stop();
});
timer.Start();
}