如何使我的WPF Textblock Ticker动画(Marquee)更流畅?

时间:2018-01-10 02:41:57

标签: c# wpf xaml marquee doubleanimation

以下文字块滚动条动画根本不流畅。所以需要一些关于如何使这个双动画更流畅以获得更多用户友好性的指导,因为我将在那之后整合RSS新闻源。

XAML代码:

<Window x:Class="Wpf_Marquee.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:Wpf_Marquee"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">

<Canvas Margin="50" Canvas.Left="150" Canvas.Top="100" ClipToBounds="True" Name="canMain" Background="Red"   Height="100" Width="400" >  

    <TextBlock x:Name="marquee"  Margin="0 50 0 0" FontSize="20pt" Foreground="Black" Background="Transparent" Canvas.Left="0" Canvas.Right="68">

        Hello This is a Sample Marquee in Wpf. Please check the animation

    </TextBlock>

    <Button x:Name="btn1" Width="50" Height="50" Content="Marquee" Click="Button_Click"   />
</Canvas>

我的.CS代码如下所示

using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel.Syndication;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Automation.Peers;
using System.Windows.Automation.Provider;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml;


namespace Wpf_Marquee
{
     /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

    }
    public void Button_Click(object sender, RoutedEventArgs e)
    {


        DoubleAnimation doubleAnimation = new DoubleAnimation();
        doubleAnimation.From = this.ActualWidth;
        doubleAnimation.To = -marquee.ActualWidth;
        doubleAnimation.RepeatBehavior = RepeatBehavior.Forever;
        doubleAnimation.Duration = new Duration(TimeSpan.FromSeconds(2060)); // provide an appropriate  duration 
        marquee.BeginAnimation(Canvas.LeftProperty, doubleAnimation);
    }


}
}

提前感谢您的帮助:)

0 个答案:

没有答案