wpf - 为什么有些文本会在 WPF 中消失,而有些则不会
问题描述
我正在使用 c# 在 Visual Studio 中创建一个 WPF 应用程序。我正在尝试使文本从一种颜色淡化为另一种颜色。有些文字会改变颜色,但有些文字会保持一种单一的颜色。代码似乎完全相同,所以我不知道我做错了什么。
// This is the working code
UserControl x:Class="MiniBiotronWPF2.Home"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:MiniBiotronWPF2"
mc:Ignorable="d"
d:DesignHeight="510" d:DesignWidth="650">
<Grid>
<TextBlock
x:Name="MyChangingColorText"
Margin="41,123,10,261" FontSize="48" FontWeight="Bold" Text="Welcome to Mini Biotron">
<TextBlock.Foreground>
<SolidColorBrush x:Name="MySolidColorBrush" Color="White"/>
</TextBlock.Foreground>
<TextBlock.Triggers>
<EventTrigger RoutedEvent="TextBlock.Loaded">
<BeginStoryboard>
<Storyboard>
<ColorAnimation
Storyboard.TargetName="MySolidColorBrush"
Storyboard.TargetProperty="Color"
From="#FF383535" To="White" Duration="0:0:3"
AutoReverse="false" RepeatBehavior="0:0:3" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</TextBlock.Triggers>
</TextBlock>
<TextBlock
x:Name="MyChangingColorText1"
Margin="188,220,156,168" FontSize="48" FontWeight="Bold" Text="Beta Edition">
<TextBlock.Foreground>
<SolidColorBrush x:Name="MySolidColorBrush1" Color="White"/>
</TextBlock.Foreground>
<TextBlock.Triggers>
<EventTrigger RoutedEvent="TextBlock.Loaded">
<BeginStoryboard>
<Storyboard>
<ColorAnimation
Storyboard.TargetName="MySolidColorBrush1"
Storyboard.TargetProperty="Color"
From="#FF383535" To="White" Duration="0:0:3"
AutoReverse="false" RepeatBehavior="0:0:3" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</TextBlock.Triggers>
</TextBlock>
//这是不褪色的程序的其余部分
<TextBlock
x:Name="MyChangingColorText2"
Margin="200,385,255,0" FontSize="16" FontWeight="Bold" Text=" Created by Lane Whitten ">
<TextBlock.Foreground>
<SolidColorBrush x:Name="MySolidColorBrush2" Color="White"/>
</TextBlock.Foreground>
<TextBlock.Triggers>
<EventTrigger RoutedEvent="TextBlock.Loaded">
<BeginStoryboard>
<Storyboard>
<ColorAnimation
Storyboard.TargetName="MySolidColorBrush1"
Storyboard.TargetProperty="Color"
From="#FF383535" To="White" Duration="0:0:3"
AutoReverse="false" RepeatBehavior="0:0:3" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</TextBlock.Triggers>
</TextBlock>
<TextBlock
x:Name="MyChangingColorText3"
Margin="200,432,25,0" FontSize="12" FontWeight="Bold" Text=" Copyright © 2018 UW BIOTRON">
<TextBlock.Foreground>
<SolidColorBrush x:Name="MySolidColorBrush3" Color="White"/>
</TextBlock.Foreground>
<TextBlock.Triggers>
<EventTrigger RoutedEvent="TextBlock.Loaded">
<BeginStoryboard>
<Storyboard>
<ColorAnimation
Storyboard.TargetName="MySolidColorBrush1"
Storyboard.TargetProperty="Color"
From="#FF383535" To="White" Duration="0:0:3"
AutoReverse="false" RepeatBehavior="0:0:3" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</TextBlock.Triggers>
</TextBlock>
<TextBlock
x:Name="MyChangingColorText4"
Margin="71,409,25,0" FontSize="12" FontWeight="Bold" Text=" For questions, concerns or to request new features contact: lanewhitten14@gmail.com ">
<TextBlock.Foreground>
<SolidColorBrush x:Name="MySolidColorBrush4" Color="White"/>
</TextBlock.Foreground>
<TextBlock.Triggers>
<EventTrigger RoutedEvent="TextBlock.Loaded">
<BeginStoryboard>
<Storyboard>
<ColorAnimation
Storyboard.TargetName="MySolidColorBrush"
Storyboard.TargetProperty="Color"
From="#FF383535" To="White" Duration="0:0:3"
AutoReverse="false" RepeatBehavior="0:0:3" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</TextBlock.Triggers>
</TextBlock>
</Grid>
</UserControl>
解决方案
您的非工作TextBlock
目标是SolidColorBrush1应该是MySolidColorBrush2,MySolidColorBrush3 ....
而是将样式定义为资源:
<Window.Resources>
<SolidColorBrush x:Key="TextBrush">Black</SolidColorBrush>
<Style TargetType="TextBlock">
<Setter Property="Foreground" Value="{StaticResource TextBrush}"/>
<Style.Triggers>
<EventTrigger RoutedEvent="TextBlock.Loaded">
<BeginStoryboard>
<Storyboard>
<ColorAnimation
Storyboard.TargetProperty="(TextBlock.Foreground).(SolidColorBrush.Color)"
From="#FF383535" To="White" Duration="0:0:3"
AutoReverse="false" RepeatBehavior="Forever" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Style.Triggers>
</Style>
</Window.Resources>
然后在同一个窗口中定义 Textblock :
<TextBlock Text="Text1"/>
推荐阅读
- ruby-on-rails - 在 OS X High Sierra 中与乘客在 apache 中的分叉问题
- swift - VC推送上的线程1错误?
- html - chrome html5 视频在一两秒后停止自动播放
- c++ - 从二进制文件中读取指定字节数时的无限循环
- c++ - C++:正则表达式模式
- parameters - 从 Declare Year、Month 和 Day Integer 传递参数值
- brightway - 在 Brightway 中注册 LCA 的影响评估方法给出类型错误
- python - SublimeText 在 virtualenv 中使用 pyflakes 对 Python3 进行 linting
- javascript - onPress 按钮在按下时不起作用
- angular - 如何在 Angular 6 中从鼠标事件中插入 CSS 效果?