对于滚动文本块,我推荐以下内容,因为 translatetransform 和剪辑不是那么流畅,让我们在 xaml 中添加:
<ScrollViewer Margin="40" Width="100" VerticalAlignment="Top" HorizontalAlignment="Left" VerticalScrollBarVisibility="Hidden" HorizontalScrollBarVisibility="Hidden" HorizontalScrollMode="Enabled">
<i:Interaction.Behaviors>
<behaviors:ScrollHorizontalBehavior/>
</i:Interaction.Behaviors>
<TextBlock Foreground="White" Text="asdfasdfasdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf HALF asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf LAST" >
</TextBlock>
</ScrollViewer>
现在转换器:
public class ScrollHorizontalBehavior : DependencyObject, IBehavior
{
public DependencyObject AssociatedObject { get; private set; }
public void Attach(DependencyObject associatedObject)
{
AssociatedObject = associatedObject;
InitializeTranslation();
}
private DispatcherTimer UITimer { get; set; }
private void InitializeTranslation()
{
var element = AssociatedObject as ScrollViewer;
UITimer = new DispatcherTimer() { Interval = TimeSpan.FromMilliseconds(30) };
UITimer.Tick += (s, e) =>
{
var newvalue = element.HorizontalOffset + 20;
if (newvalue > element.ScrollableWidth)
newvalue = 0;
element.ChangeView(newvalue, null, null);
};
UITimer.Start();
}
public void Detach()
{
if (UITimer != null) UITimer.Stop();
}
}
如您所见,这种方式最好的方式是,您可以管理滚动结束时要执行的操作。
现在添加闪烁行为
<TextBlock Foreground="White" Text="asdfasdfasdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf HALF asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf LAST" >
<i:Interaction.Behaviors>
<behaviors:BlinkingBehavior/>
</i:Interaction.Behaviors>
</TextBlock>
行为更容易的地方:
public class BlinkingBehavior : DependencyObject, IBehavior
{
public DependencyObject AssociatedObject { get; private set; }
public void Attach(DependencyObject associatedObject)
{
AssociatedObject = associatedObject;
InitializeBlinking();
}
bool firstcolor = true;
private void InitializeBlinking()
{
var element = AssociatedObject as TextBlock;
var brushA = new SolidColorBrush(Colors.Red);
var brushB = new SolidColorBrush(Colors.White);
UITimer = new DispatcherTimer() { Interval = TimeSpan.FromMilliseconds(1000) };
UITimer.Tick += (s, e) =>
{
element.Foreground = firstcolor ? brushA : brushB;
firstcolor = !firstcolor;
};
UITimer.Start();
}
private DispatcherTimer UITimer { get; set; }
public void Detach()
{
if (UITimer != null) UITimer.Stop();
}
}
注意:我是为 Windows 10 做的,所以在你的情况下它可能会有所改变。我需要一点时间来完成它,所以如果你觉得它真的有用,请标记为答案。