silverlight - 混合行为 (Silverlight 4)

标签 silverlight xaml expression-blend behavior

我想知道是否有人知道 Blend/Silverlight 4 的任何好的(免费)行为

具体来说,我正在寻找一种可以放在 TextBlock 上的行为
使其水平滚动或在 TextBlock 中“闪烁”文本(闪烁文本)的行为。但我很想听听您一直在使用或了解的任何行为。

例如,我有一个非常基本的“闪烁文本”行为

public class FlashTextBehavior : Behavior<TextBlock>
{
    Timer flashTimer;

    public FlashTextBehavior()
    {

    }

    protected override void OnAttached()
    {
        base.OnAttached();
        flashTimer = new Timer(new TimerCallback((o) => 
        {
            Dispatcher.BeginInvoke(() =>
            {
                if (AssociatedObject.Visibility == Visibility.Visible)
                    AssociatedObject.Visibility = Visibility.Collapsed;
                else
                    AssociatedObject.Visibility = Visibility.Visible;
            });               
        }), null, 0, 750);
    }

    protected override void OnDetaching()
    {
        if (flashTimer != null)
            flashTimer.Dispose();

        base.OnDetaching();
    }
}

当然它可以改进,但我真的很感兴趣其他什么
人们已经想出了。

最佳答案

对于滚动文本块,我推荐以下内容,因为 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 做的,所以在你的情况下它可能会有所改变。我花了一点时间来完成它,如果您觉得它真的有用,请标记为答案。

关于silverlight - 混合行为 (Silverlight 4),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2957873/

相关文章:

wpf - 在复杂、解耦的 WPF 应用程序上使用 Expression Blend 的策略是什么?

c# - Caliburn.Micro - 我可以处理一个 Action 但继续将它发送到可视化树上吗?

WPF ListView VerticalScrollBar 未显示

wpf - 使用资源进行主题化而不需要呕吐

wpf - 文本从左侧修剪

c# - WPF 筛选列表框

wpf - 在内容中制作带有矢量图形的按钮

c# - Silverlight Storyboard中的收缩面板

silverlight - Silverlight 视频播放器可以播放 Azure blob 中的 wmv 吗?

silverlight - 在 Silverlight 中串行处理多个异步 HTTP 请求