/// <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) { } }
public void InitializeFoodsAndFacilitiesLayout() { if (Children == null) return; if (Children.Count < 2) return; try { _mainFragment = Children.OfType<Grid>().FirstOrDefault(); _listFragment = Children.OfType<Grid>().ElementAt(1); } catch { return; } if (_mainFragment == null || _listFragment == null) return; _mainFragment.Name = "_mainFragment"; _listFragment.Name = "_listFragment"; // _mainFragment _mainFragment.HorizontalAlignment = HorizontalAlignment.Stretch; _mainFragment.VerticalAlignment = VerticalAlignment.Stretch; if (_mainFragment.Background == null) _mainFragment.Background = new SolidColorBrush(Color.FromArgb(0, 0, 0, 0)); // Render transform _listFragment _listFragment.HorizontalAlignment = HorizontalAlignment.Left; _listFragment.VerticalAlignment = VerticalAlignment.Stretch; _listFragment.Width = (Window.Current.Bounds.Width/3)*2; if (_listFragment.Background == null) _listFragment.Background = new SolidColorBrush(Color.FromArgb(255, 255, 255, 255)); var animatedTranslateTransform = new TranslateTransform {X = -_listFragment.Width, Y = 0}; _listFragment.RenderTransform = animatedTranslateTransform; _listFragment.RenderTransformOrigin = new Point(0.5, 0.5); _listFragment.UpdateLayout(); // Create a shadow element _shadowFragment = new Grid { Name = "_shadowFragment", Background = new SolidColorBrush(Color.FromArgb(0, 255, 255, 255)), HorizontalAlignment = HorizontalAlignment.Stretch, VerticalAlignment = VerticalAlignment.Stretch, Visibility = Visibility.Collapsed }; _shadowFragment.Tapped += shadowFragment_Tapped; _shadowFragment.IsHitTestVisible = false; // Set ZIndexes Canvas.SetZIndex(_shadowFragment, 50); Canvas.SetZIndex(_listFragment, 51); Children.Add(_shadowFragment); // Create a new fadeIn animation storyboard _fadeInStoryboard = new Storyboard(); // New double animation var doubleAnimation1 = new DoubleAnimation {Duration = new Duration(new TimeSpan(0, 0, 0, 0, 200)), To = 0}; Storyboard.SetTarget(doubleAnimation1, _listFragment); Storyboard.SetTargetProperty(doubleAnimation1, TranslatePath.Path); _fadeInStoryboard.Children.Add(doubleAnimation1); // New color animation for _shadowFragment var colorAnimation1 = new ColorAnimation { Duration = new Duration(new TimeSpan(0, 0, 0, 0, 200)), To = Color.FromArgb(190, 0, 0, 0) }; Storyboard.SetTarget(colorAnimation1, _shadowFragment); Storyboard.SetTargetProperty(colorAnimation1,ColorPath.Path); _fadeInStoryboard.Children.Add(colorAnimation1); // Create a new fadeOut animation storyboard _fadeOutStoryboard = new Storyboard(); // New double animation var doubleAnimation2 = new DoubleAnimation { Duration = new Duration(new TimeSpan(0, 0, 0, 0, 200)), To = -_listFragment.Width }; Storyboard.SetTarget(doubleAnimation2, _listFragment); Storyboard.SetTargetProperty(doubleAnimation2, TranslatePath.Path); _fadeOutStoryboard.Children.Add(doubleAnimation2); // New color animation for _shadowFragment var colorAnimation2 = new ColorAnimation { Duration = new Duration(new TimeSpan(0, 0, 0, 0, 200)), To = Color.FromArgb(0, 0, 0, 0) }; Storyboard.SetTarget(colorAnimation2, _shadowFragment); Storyboard.SetTargetProperty(colorAnimation2, ColorPath.Path); _fadeOutStoryboard.Children.Add(colorAnimation2); _mainFragment.ManipulationMode = ManipulationModes.All; _mainFragment.ManipulationStarted += mainFragment_ManipulationStarted; _listFragment.ManipulationMode = ManipulationModes.All; _listFragment.ManipulationStarted += listFragment_ManipulationStarted; }
private bool Handling = false;//是否正在监听 /// <summary> /// 添加顶部及底部弹幕 /// </summary> /// <param name="model">弹幕参数</param> /// <param name="istop">是否顶部</param> /// <param name="Myself">是否自己发送的</param> public async void AddTopButtomDanmu(DanMuModel model, bool istop,bool Myself) { TextBlock tx = new TextBlock(); TextBlock tx2 = new TextBlock(); Grid grid = new Grid(); 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;//new SolidColorBrush(co[rd.Next(0, 7)]); 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); // tx.FontSize = Double.Parse(model.DanSize) - fontSize; grid.HorizontalAlignment = HorizontalAlignment.Center; grid.VerticalAlignment = VerticalAlignment.Top; tx2.Margin = new Thickness(1); if (Myself) { grid.BorderThickness = new Thickness(2); grid.BorderBrush = new SolidColorBrush(Colors.Gray); } grid.Opacity = Tran; grid.DataContext = model; grid.UpdateLayout(); if (istop) { D_Top.Children.Add(grid); await Task.Delay(5000); if (state== MediaElementState.Paused) { ClearTopButtomDanmuku(); } else { D_Top.Children.Remove(grid); } } else { D_Bottom.Children.Add(grid); await Task.Delay(5000); if (state == MediaElementState.Paused) { ClearTopButtomDanmuku(); } else { D_Bottom.Children.Remove(grid); } } }