There is such a code (wrapper) for MediaElement'a, and that's when I noticed a lot of new initializations of the class EventHandler'a thought, is this correct? Actually the code below. Is there another way to do the same without creating a new EventHandler each time?

namespace Mynamespace.Controls { public class AnimePlayer : Border { public delegate void StateChangedEventHandler(object sender, AnimePlayerEventArgs animePlayerEventArgs); private readonly IMessageService _messageService; public AnimePlayer(IMessageService messageService) : this( messageService, new MediaElement {LoadedBehavior = MediaState.Manual, UnloadedBehavior = MediaState.Manual}) { } public AnimePlayer(IMessageService messageService, MediaElement mediaElement) { _messageService = messageService; MediaElement = mediaElement; CreateControl(); } private MediaElement MediaElement { get; } public event StateChangedEventHandler PlayerStateChanged; private void CreateControl() { Background = Brushes.Black; Child = new Grid { RowDefinitions = {new RowDefinition(), new RowDefinition {Height = GridLength.Auto}}, ColumnDefinitions = {new ColumnDefinition(), new ColumnDefinition()}, Children = {MediaElement} }; MediaElement.HorizontalAlignment = HorizontalAlignment.Center; MediaElement.VerticalAlignment = VerticalAlignment.Center; Grid.SetColumnSpan(MediaElement, 2); Grid.SetRowSpan(MediaElement, 2); MediaElement.MediaFailed += async (sender, args) => await _messageService.ShowErrorAsync(args.ErrorException); MediaElement.MediaOpened += (sender, args) => OnPlayerStateChanged(new AnimePlayerEventArgs(PlayerState.Playing)); MediaElement.BufferingStarted += (sender, args) => { OnPlayerStateChanged(new AnimePlayerEventArgs(PlayerState.Buffering, MediaElement.BufferingProgress)); }; MediaElement.MediaEnded += (sender, args) => { OnPlayerStateChanged(new AnimePlayerEventArgs(PlayerState.Stopped)); MediaElement.Stop(); MediaElement.Position = TimeSpan.Zero; }; } public void Play() { if (!MediaElement.HasAudio && !MediaElement.HasVideo && MediaElement.Source == null) return; OnPlayerStateChanged(new AnimePlayerEventArgs(PlayerState.Playing)); MediaElement.Play(); } public void Play(Uri mediaUri) { OpenMedia(mediaUri); Play(); } public void Pause() { if (!MediaElement.HasAudio && !MediaElement.HasVideo && MediaElement.Source == null) return; OnPlayerStateChanged(new AnimePlayerEventArgs(PlayerState.Paused)); MediaElement.Pause(); } public void OpenMedia(Uri mediaUri) { MediaElement.Source = mediaUri; } public void OpenMedia(Uri mediaUri, bool playOnOpen) { if (playOnOpen) { Play(mediaUri); return; } OpenMedia(mediaUri); } public void Close() { MediaElement.Close(); } /// <summary> /// Происходит при изменении StateNow /// </summary> /// <param name="animeplayereventargs"></param> protected virtual void OnPlayerStateChanged(AnimePlayerEventArgs animeplayereventargs) { PlayerStateChanged?.Invoke(this, animeplayereventargs); } public class AnimePlayerEventArgs : EventArgs { public AnimePlayerEventArgs() { } public AnimePlayerEventArgs(PlayerState playerState) { StateNow = playerState; } public AnimePlayerEventArgs(PlayerState playerState, double progressBuffering) : this(playerState) { BufferingProgress = progressBuffering; } /// <summary> /// Что делает проигрыватель /// </summary> public PlayerState StateNow { get; internal set; } public double BufferingProgress { get; internal set; } } } /// <summary> /// Состояние аниме проигрывателя /// </summary> public enum PlayerState { /// <summary> /// Проигрывает /// </summary> Playing, /// <summary> /// Приостоновлен /// </summary> Paused, /// <summary> /// Бефферизация /// </summary> Buffering, /// <summary> /// Медиа закрыто, простаивает /// </summary> Closed, /// <summary> /// Остановлен /// </summary> Stopped, /// <summary> /// Открытие мультимедиа /// </summary> MediaOpen } } 
  • 2
    Where do you see "many new instances of the EventHandler class" ? - andreycha
  • It means a lot of initializations, I would like to get rid of it. - LLENN
  • I also could not find them in the code. Could you please formulate the question specifically? - Uranus
  • @Uranus The very essence of the question in the title, I mean a lot of initializations, I would like to get rid of it, the thought of static came to my mind, but would it make sense? - LLENN
  • 2
    It's just that the only instance class that creates and initializes the code given in the question in quantities suitable for the wording "constantly" and "set" is AnimePlayerEventArgs. If you are talking about him, then do it right. - Uranus

0