Windows Phone 7中的彩色动画 [英] COLOUR ANIMATION in windows phone 7
本文介绍了Windows Phone 7中的彩色动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
< phone:PhoneApplicationPage.Resources >
< 故事板 =" Storyboard1" < ColorAnimationUsingKeyFrames =" (TextBlock.Foreground).(SolidColorBrush.Color)" Storyboard.TargetName =" textBlock" < EasingColorKeyFrame =" 0:0:2.5" 值 =" 红色" / >
< EasingColorKeyFrame =" 0:0:5" 值 =" 蓝色" / >
< EasingColorKeyFrame =" 0:0:7.5" 值 =" 红色" / >
< EasingColorKeyFrame =" 0:0:10" 值 =" 白色" / >
< /ColorAnimationUsingKeyFrames >
< /Storyboard >
< /phone:PhoneApplicationPage.Resources > 我认为,您不能使用这种方式;)因为foreground
是TextBlock
中所有文本的属性.
我的解决方案:
创建TextBlock
的数组",例如:
TextBlock [] tb = 新 TextBlock [ 5 ]; 然后创建一个计时器,最后在每个滴答事件发生时,更改foreground
数组元素之一.
XAML中的更新程序:
< Window x:Class ="
span> xmlns =" http://schemas.microsoft.com/winfx/2006/xaml/presentation"
span> xmlns:x =" http://schemas.microsoft.com/winfx/2006/xaml"
span> xmlns:System =" clr-namespace:System; assembly = mscorlib"
span> 标题 =" MainWindow" 高度 350" Width =" WindowStartupLocation =" >
< Window.Resources >
< System:String x:Key =" > textBlock0 < /System:String >
< 故事板 =" Storyboard1" < ColorAnimationUsingKeyFrames =" (TextBlock.Foreground).(SolidColorBrush.Color)" Storyboard.TargetName =" {DynamicResource textBlockName}" < EasingColorKeyFrame =" 0:0:2.5" 值 =" 红色" / >
< EasingColorKeyFrame =" 0:0:5" 值 =" 蓝色" / >
< EasingColorKeyFrame =" 0:0:7.5" 值 =" 红色" / >
< EasingColorKeyFrame =" 0:0:10" 值 =" 白色" / >
< /ColorAnimationUsingKeyFrames >
< /Storyboard >
< /Window.Resources >
< Viewbox >
< StackPanel =" TextBlocks" 方向 水平" >
< /StackPanel >
< /Viewbox >
< /Window >
然后在C#中编写代码:
使用系统;
使用 System.Linq;
使用 System.Windows;
使用使用System.Windows.Controls;
使用 System.Windows.Media;
使用使用System.Windows.Media.Animation;
命名空间 WpfApp_ColourTextBlock
{
/// < >
/// MainWindow.xaml的交互逻辑
/// < /summary >
公共 部分 类 MainWindow : 窗户
{
公共 MainWindow()
{
InitializeComponent();
counter = 0 ;
foreach ( char 中的 in 中的项目" .ToArray())
{
TextBlock tb = 新 TextBlock();
tb.Name = " + counter ++;
此 .RegisterName(tb.Name,tb);
tb.Text = item.ToString();
tb.Foreground = Brushes.DarkCyan;
TextBlocks.Children.Add(tb);
}
counter = 0 ;
System.Windows.Threading.DispatcherTimer myDispatcherTimer = 新 System.Windows.Threading.DispatcherTimer();
myDispatcherTimer.Interval = 新 TimeSpan( 0 , 0 , 0 , 0 , 1000 ); // 100毫秒
myDispatcherTimer.Tick + = 新 EventHandler(myDispatcherTimer_Tick);
myDispatcherTimer.Start();
}
int 计数器;
无效 myDispatcherTimer_Tick(对象发送者,EventArgs e)
{
资源[" ] = " textBlock" +计数器++;
(资源[" ] > 故事板).Begin();
如果(计数器> = TextBlocks.Children.Count)
counter = 0 ;
}
}
}
<phone:PhoneApplicationPage.Resources>
<Storyboard x:Name="Storyboard1">
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(TextBlock.Foreground).(SolidColorBrush.Color)" Storyboard.TargetName="textBlock">
<EasingColorKeyFrame KeyTime="0:0:2.5" Value="Red"/>
<EasingColorKeyFrame KeyTime="0:0:5" Value="Blue"/>
<EasingColorKeyFrame KeyTime="0:0:7.5" Value="Red"/>
<EasingColorKeyFrame KeyTime="0:0:10" Value="White"/>
</ColorAnimationUsingKeyFrames>
</Storyboard>
</phone:PhoneApplicationPage.Resources>
解决方案 I think, you cant use this way;) because foreground
is property for all of the text in the TextBlock
.
My solution:
Create ''Array'' of TextBlock
for example:
TextBlock[] tb = new TextBlock[5];
then create a timer and finally with every tick event, change foreground
one of array element.
Updated program in XAML:
<Window x:Class="WpfApp_ColourTextBlock.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:System="clr-namespace:System;assembly=mscorlib"
Title="MainWindow" Height="350" Width="525" WindowStartupLocation="CenterScreen">
<Window.Resources>
<System:String x:Key="textBlockName">textBlock0</System:String>
<Storyboard x:Key="Storyboard1">
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(TextBlock.Foreground).(SolidColorBrush.Color)" Storyboard.TargetName="{DynamicResource textBlockName}">
<EasingColorKeyFrame KeyTime="0:0:2.5" Value="Red"/>
<EasingColorKeyFrame KeyTime="0:0:5" Value="Blue"/>
<EasingColorKeyFrame KeyTime="0:0:7.5" Value="Red"/>
<EasingColorKeyFrame KeyTime="0:0:10" Value="White"/>
</ColorAnimationUsingKeyFrames>
</Storyboard>
</Window.Resources>
<Viewbox>
<StackPanel Name="TextBlocks" Orientation="Horizontal">
</StackPanel>
</Viewbox>
</Window>
And code behind in C#:
using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;
namespace WpfApp_ColourTextBlock
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
counter = 0;
foreach (char item in "Hello Word".ToArray())
{
TextBlock tb = new TextBlock();
tb.Name = "textBlock" + counter++;
this.RegisterName(tb.Name, tb);
tb.Text = item.ToString();
tb.Foreground = Brushes.DarkCyan;
TextBlocks.Children.Add(tb);
}
counter = 0;
System.Windows.Threading.DispatcherTimer myDispatcherTimer = new System.Windows.Threading.DispatcherTimer();
myDispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 1000); // 100 Milliseconds
myDispatcherTimer.Tick += new EventHandler(myDispatcherTimer_Tick);
myDispatcherTimer.Start();
}
int counter;
void myDispatcherTimer_Tick(object sender, EventArgs e)
{
Resources["textBlockName"] = "textBlock" + counter++;
(Resources["Storyboard1"] as Storyboard).Begin();
if (counter >= TextBlocks.Children.Count)
counter = 0;
}
}
}
这篇关于Windows Phone 7中的彩色动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文