2010-06-02 63 views
5

我不知道是否有人知道的任何混合好的(免費)行爲/ Silverlight 4中行爲對於混合(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(); 
    } 
} 

當然,它可以改進,但我在其他什麼人 紛紛拿出真正有興趣。

+0

通過「水平滾動」,你的意思是自動像一個自動收錄機或其他東西? – 2010-08-19 19:40:30

+0

我的意思是使文本在文本框內像滾動字幕一樣滾動 – TimothyP 2010-08-20 08:11:07

回答

1

滾動以下筆者推薦的文本塊,因爲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,所以它可以在你的情況就發生了變化。這需要我做一點,所以如果你發現它真的有用,請給出答案。

相關問題