微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

混合行为(Silverlight 4)

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

具体来说,我正在寻找一种可以放在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,750);
    }

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

        base.OnDetaching();
    }
}

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

解决方法

为了滚动文本块,我推荐以下内容,因为translatetransform和clip不是那么顺利,让我们在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);
        };
        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做的,因此在您的情况下可能会有所改变.它需要我做一点,所以如果你觉得它真的很有用,请标记为答案.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐