環 境: Windows 8 32位 企業版 開發工具: Visual Studio Express 2012 for Windows 8 語 言: C# XAML 技 術: 色彩漸層
XAML
代碼設定:
<TextBlock Text="你是我的眼" FontSize="45" Height="200" Width="500" >
<TextBlock.Foreground>
<LinearGradientBrush>
<GradientStop Color="Red" Offset="0"></GradientStop>
<GradientStop x:Name="gs1" Color="Red"Offset="0"></GradientStop>
<GradientStop x:Name="gs2" Color="Green"Offset="0"></GradientStop>
<GradientStop Color="Green" Offset="1"></GradientStop>
</LinearGradientBrush>
</TextBlock.Foreground>
</TextBlock>
C#代碼設定:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
DispatcherTimer timer = new DispatcherTimer(); //建立定時器
timer.Interval = TimeSpan.FromMilliseconds(200); //設定間隔為0.2秒
timer.Tick+=timer_Tick;
timer.Start(); //開啟定時器
}
void timer_Tick(object sender, object e)
{
gs1.Offset += 0.05;
gs2.Offset += 0.05;
}
運行效果:
其中的x:Name屬性類似於附加屬性,GradientStop本來沒有Name屬性,所以在XAML中用上面形式附加屬性,便於在C#中進行操作,達到控制的效果。