/// <summary> /// 添加滚动弹幕 /// </summary> /// <param name="model">弹幕参数</param> /// <param name="Myself">是否自己发送的</param> public async void AddGunDanmu(DanMuModel model,bool Myself) { try { //创建基础控件 TextBlock tx = new TextBlock(); TextBlock tx2 = new TextBlock(); Grid grid = new Grid(); //设置控件相关信息 grid.Margin = new Thickness(0, 0, 20, 0); grid.VerticalAlignment = VerticalAlignment.Center; grid.HorizontalAlignment = HorizontalAlignment.Left; if (fontFamily != "默认") { tx.FontFamily = new FontFamily(fontFamily); tx2.FontFamily = new FontFamily(fontFamily); } tx2.Text = model.DanText; tx.Text = model.DanText; tx2.Foreground = new SolidColorBrush(Color.FromArgb(255, 0, 0, 0)); tx.Foreground = model.DanColor; //弹幕大小 double size = double.Parse(model.DanSize); if (size == 25) { tx2.FontSize = fontSize; tx.FontSize = fontSize; } else { tx2.FontSize = fontSize - 2; tx.FontSize = fontSize - 2; } //grid包含弹幕文本信息 grid.Children.Add(tx2); grid.Children.Add(tx); grid.VerticalAlignment = VerticalAlignment.Top; grid.HorizontalAlignment = HorizontalAlignment.Left; TranslateTransform moveTransform = new TranslateTransform(); moveTransform.X = grid_Danmu.ActualWidth; grid.RenderTransform = moveTransform; //将弹幕加载入控件中,并且设置位置 grid_Danmu.Children.Add(grid); Grid.SetRow(grid, row); row++; if (row == maxRow) { row = 0; } tx2.Margin = new Thickness(1); if (Myself) { grid.BorderThickness = new Thickness(2); grid.BorderBrush = new SolidColorBrush(Colors.Gray); } grid.Opacity = Tran; grid.DataContext = model; //更新弹幕UI,不更新无法获得弹幕的ActualWidth grid.UpdateLayout(); //创建动画 Duration duration = new Duration(TimeSpan.FromSeconds(Speed)); DoubleAnimation myDoubleAnimationX = new DoubleAnimation(); myDoubleAnimationX.Duration = duration; //创建故事版 Storyboard justintimeStoryboard = new Storyboard(); justintimeStoryboard.Duration = duration; myDoubleAnimationX.To = -(grid.ActualWidth);//到达 justintimeStoryboard.Children.Add(myDoubleAnimationX); Storyboard.SetTarget(myDoubleAnimationX, moveTransform); //故事版加入动画 Storyboard.SetTargetProperty(myDoubleAnimationX, "X"); grid_Danmu.Resources.Remove("justintimeStoryboard"); grid_Danmu.Resources.Add("justintimeStoryboard", justintimeStoryboard); justintimeStoryboard.Begin(); //等待,暂停则暂停 await Task.Run(async () => { int i = 0; while (true) { if (!IsPlaying) { await this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { justintimeStoryboard.Pause(); }); //break; } else { if (i == Speed*2) { break; } i++; await this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { justintimeStoryboard.Resume(); }); } await Task.Delay(500); } }); grid_Danmu.Children.Remove(grid); } catch (Exception) { } }