wpf 字幕动画切换问题

zhantianyou 2012-06-28 04:20:22
各位大虾好:
我的首次字幕如下图正常的

但当我在25秒切换另一个字幕的时候,动画不动了,就算动也一顿一顿的,过10几少之后,才正常 ,请问是什么原因啊。


求救啊,项目急着要!
...全文
163 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhantianyou 2012-06-28
  • 打赏
  • 举报
回复
private double MeasureTextWidth()
{
FormattedText formattedText = new FormattedText(
this.marquee.Content.ToString(),
System.Globalization.CultureInfo.InvariantCulture,
FlowDirection.LeftToRight,
new Typeface(this.marquee.FontFamily.ToString().TrimEnd()),
this.marquee.FontSize,
Brushes.Black
);
return formattedText.WidthIncludingTrailingWhitespace;
}

private double MeasureTextHeight()
{
FormattedText formattedText = new FormattedText(
this.marquee.Content.ToString(),
System.Globalization.CultureInfo.InvariantCulture,
FlowDirection.LeftToRight,
new Typeface(this.marquee.FontFamily.ToString().TrimEnd()),
this.marquee.FontSize,
Brushes.Black
);
return formattedText.Height;
}

private void Button_Click(object sender, RoutedEventArgs e)
{
Stop();
}

private void Button_Click_1(object sender, RoutedEventArgs e)
{
DispatcherTimer timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromSeconds(25);
timer.Tick += new EventHandler(timer_Tick);
timer.IsEnabled = true;
Stop();
setContent("jfkdsajfkasjkfjaksjflasjfkasjflkajsflkjaslkfdjalskjflaksjflkasjflkajslfkjaslkfjalskjfklasjdfkajfkj");
run();
}

void timer_Tick(object sender, EventArgs e)
{
Stop();
setContent("testestekasdjfksajfkasj");
run();
DispatcherTimer timer = sender as DispatcherTimer;
timer.IsEnabled = false;
}

private void Button_Click_2(object sender, RoutedEventArgs e)
{

Stop();
setContent("testestekasdjfksajfkasj");
run();
}
zhantianyou 2012-06-28
  • 打赏
  • 举报
回复
后台代码
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
private Storyboard story;
private Storyboard story2;
private double width = 0;
private double height = 0;

private double rowHeight = 0;

private int speed = 0;
private int runType = 1;
private int index = 0;
private List<string> listText;
private string[] listContent;

private double playTo = 0;
private double playFrom = 0;
private double playTo2 = 0;
private double playFrom2 = 0;

private double textHeight = 0.0;

public MainWindow()
{
InitializeComponent();

}

public void run()
{

if (this.runType == 0)
{
}
else
{
this.playY();
}
}

private object m_LockObject = new object();

public void Stop()
{
if (this.story != null)
{
this.story.Name = "";
this.story.Stop(this);
this.story = null;
}

if (this.story2 != null)
{
this.story2.Name = "";
this.story2.Stop(this);
this.story2 = null;
}
}

//上翻跑马灯
private void playY()
{
this.story = new Storyboard();

this.story2 = new Storyboard();

this.marquee.Background = Brushes.Pink;
this.marquee2.Background = Brushes.Green;
this.grid.Background = Brushes.Yellow;

int intRuntime = 1;

this.getContent(this.marquee.Content.ToString());

this.textHeight = this.MeasureTextHeight();
this.marquee.Height = this.textHeight;
this.marquee2.Height = this.textHeight;
this.grid.Height = this.textHeight;
for (int i = 0; i < this.listContent.Length; i++)
{
if (i == 0)
{
marquee.Content = this.listContent[i];
}
else
{
marquee.Content += Environment.NewLine + this.listContent[i];
}
}
marquee2.Content = marquee.Content;

this.index++;

this.rowHeight = this.textHeight / this.listContent.Length;
this.playFrom = this.rowHeight;
this.playTo = 0;

DoubleAnimation animation = new DoubleAnimation();
animation.Duration = TimeSpan.FromSeconds(intRuntime);
animation.From = this.playFrom;
animation.To = this.playTo;

this.story2.Name = "story2";
this.story.Name = "story";

Storyboard.SetTargetName(animation, "lab");
Storyboard.SetTargetProperty(animation, new PropertyPath(TranslateTransform.YProperty));
this.story.Duration = TimeSpan.FromSeconds(intRuntime);
this.story.Children.Add(animation);
this.story.FillBehavior = FillBehavior.Stop;
this.story.Completed += new EventHandler(story_Completed);
this.story.Begin(this, true);



this.playFrom2 = this.playFrom + this.textHeight;
this.playTo2 = this.playFrom2 - this.rowHeight;
DoubleAnimation animation2 = new DoubleAnimation();
animation2.Duration = TimeSpan.FromSeconds(intRuntime);
animation2.From = playFrom2;
animation2.To = playTo2;
Storyboard.SetTargetName(animation2, "lab2");
Storyboard.SetTargetProperty(animation2, new PropertyPath(TranslateTransform.YProperty));
this.story2.Duration = TimeSpan.FromSeconds(intRuntime);
this.story2.Children.Add(animation2);
this.story2.FillBehavior = FillBehavior.Stop;
this.story2.Completed += new EventHandler(story2_Completed);
this.story2.Begin(this, true);
}

public void setFont(FontWeight font)
{
this.marquee.FontWeight = font;
this.marquee2.FontWeight = font;
}

public void setContent(string content)
{
this.marquee.Content = content;
double gridWidth = MeasureTextWidth();
if (this.grid.Width < gridWidth)
{
this.grid.Width = gridWidth;
}
this.marquee2.Content = content;
}

public void setFontColor(Color color)
{
this.marquee.Foreground = new SolidColorBrush(color);
this.marquee2.Foreground = new SolidColorBrush(color);
}

public void setBackground(Color color)
{
this.Background = new SolidColorBrush(color);
this.grid.Background = new SolidColorBrush(color);
this.marquee.Background = new SolidColorBrush(color);
this.marquee2.Background = new SolidColorBrush(color);
}

public void setFontFamily(FontFamily fontFamily)
{
this.marquee.FontFamily = fontFamily;
this.marquee2.FontFamily = fontFamily;
}

public void setFontSize(int size)
{
this.marquee.FontSize = size;
this.marquee2.FontSize = size;
}

public void setWidth(int width)
{
this.width = width;
this.Width = width;
}

public void setHeight(int height)
{
this.height = height;
this.Height = height;
}

public void setSpeed(int speed)
{
this.speed = speed;
}

public void setRunType(int runType)
{
this.runType = runType;
}

public void setListText(List<string> listText)
{
this.listText = listText;
}

private void getContent(string content)
{
this.listContent = content.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
}

private void story_Completed(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(this.story.Name))
{
return;
}

Console.WriteLine("上次From1-> " + this.playFrom.ToString() + " To->" + this.playTo);
double span = Math.Abs(this.textHeight - Math.Abs(this.playTo));

if (span >= this.rowHeight)
{
this.playFrom = this.playTo;
this.playTo -= this.rowHeight;
}
else
{
if (this.listContent.Length == 1)
{
this.playFrom = this.rowHeight;
}
else
{
this.playFrom = this.playTo2 + this.textHeight;
}
this.playTo = this.playFrom - this.rowHeight;
}

if (string.IsNullOrEmpty(this.story2.Name))
{
return;
}

Console.WriteLine("这次From1-> " + this.playFrom.ToString() + " To->" + this.playTo);
DoubleAnimation animation = (DoubleAnimation)this.story.Children[0];
animation.From = this.playFrom;
animation.To = this.playTo;
this.story.BeginTime = TimeSpan.FromSeconds(5);
this.story.Begin(this);
}

private void story2_Completed(object sender, EventArgs e)
{
Console.WriteLine("上次From2-> " + this.playFrom2.ToString() + " To->" + this.playTo2);
double span = this.playTo2 + this.textHeight;
if (span >= this.rowHeight)
{
this.playFrom2 = this.playTo2;
this.playTo2 -= this.rowHeight;
}
else
{
this.playFrom2 = this.playFrom + this.textHeight;
this.playTo2 = this.playFrom2 - this.rowHeight;
}
Console.WriteLine("这次From2-> " + this.playFrom2.ToString() + " To->" + this.playTo2);
DoubleAnimation animation = (DoubleAnimation)this.story2.Children[0];
animation.From = this.playFrom2;
animation.To = this.playTo2;
this.story2.BeginTime = TimeSpan.FromSeconds(5);
this.story2.Begin(this);
}
}
zhantianyou 2012-06-28
  • 打赏
  • 举报
回复
以下是我的UI代码
<Window
x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
x:Name="self" Width="800" Height="600" >
<Grid >
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Grid Height="75" Background="Yellow">
<Grid Name="grid" Background="#00000000" HorizontalAlignment="Left" VerticalAlignment="Top">
<Label Name="marquee" HorizontalAlignment="Left" HorizontalContentAlignment="Left" VerticalAlignment="Top"
VerticalContentAlignment="Top" Padding="0" BorderThickness ="0" FontSize="29" Background="#00000000">
<Label.RenderTransform>
<TranslateTransform x:Name="lab"/>
</Label.RenderTransform>
</Label>
<Label Name="marquee2" HorizontalAlignment="Left" HorizontalContentAlignment="Left" FontSize="29" VerticalAlignment="Top"
VerticalContentAlignment="Top" Padding="0" BorderThickness ="0" Background="#00000000" >
<Label.RenderTransform>
<TranslateTransform x:Name="lab2"/>
</Label.RenderTransform>
</Label>
</Grid>
</Grid>
<StackPanel Grid.Row="1">
<Button Height="23" Margin="0" Click="Button_Click">动画</Button>
<Button Height="23" Margin="0" Click="Button_Click_1">文本</Button>
<Button Height="23" Margin="0" Click="Button_Click_2">改变文本</Button>
</StackPanel>
</Grid>
</Window>

110,535

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧