Commit f4956e8b authored by Martin Finkel's avatar Martin Finkel

WPF: Use a DependencyProperty

Move the MediaPlayerEventManager in LibVLCSharp since it's used by WPF in addition to Forms
parent a939717a
using LibVLCSharp.Forms.Platforms.WPF;
using LibVLCSharp.Shared;
using LibVLCSharp.Forms.Platforms.WPF;
using LibVLCSharp.Forms.Shared;
using Xamarin.Forms.Platform.WPF;
......
using Android.Content;
using LibVLCSharp.Shared;
using LibVLCSharp.Forms.Platforms.Android;
using LibVLCSharp.Forms.Shared;
......
using LibVLCSharp.Forms.Platforms.Mac;
using LibVLCSharp.Shared;
using LibVLCSharp.Forms.Platforms.Mac;
using LibVLCSharp.Forms.Shared;
using Xamarin.Forms;
......
using LibVLCSharp.Forms.Platforms.iOS;
using LibVLCSharp.Shared;
using LibVLCSharp.Forms.Platforms.iOS;
using LibVLCSharp.Forms.Shared;
using Xamarin.Forms;
......
using System;
namespace LibVLCSharp.Forms.Shared
{
public class MediaPlayerChangedEventArgs : EventArgs
{
public MediaPlayerChangedEventArgs(LibVLCSharp.Shared.MediaPlayer oldMediaPlayer, LibVLCSharp.Shared.MediaPlayer newMediaPlayer)
{
OldMediaPlayer = oldMediaPlayer;
NewMediaPlayer = newMediaPlayer;
}
public LibVLCSharp.Shared.MediaPlayer OldMediaPlayer { get; }
public LibVLCSharp.Shared.MediaPlayer NewMediaPlayer { get; }
}
}
\ No newline at end of file
using System;
using LibVLCSharp.Shared;
using System;
using System.Diagnostics;
using Xamarin.Forms;
......
......@@ -19,36 +19,27 @@ namespace LibVLCSharp.WPF
{
DefaultStyleKey = typeof(VideoView);
}
public static readonly DependencyProperty MediaPlayerProperty = DependencyProperty.Register(nameof(MediaPlayer),
typeof(MediaPlayer),
typeof(VideoView),
new PropertyMetadata(null, OnMediaPlayerChanged));
private void Attach()
public MediaPlayer MediaPlayer
{
if (!IsDesignMode)
{
if (MediaPlayer == null)
{
Trace.Write("No MediaPlayer is set, aborting...");
return;
}
var hwnd = (Template.FindName(PART_PlayerView, this) as System.Windows.Forms.Panel)?.Handle;
if (hwnd == null)
{
Trace.WriteLine("HWND is NULL, aborting...");
return;
}
MediaPlayer.Hwnd = (IntPtr)hwnd;
}
get { return GetValue(MediaPlayerProperty) as MediaPlayer; }
set { SetValue(MediaPlayerProperty, value); }
}
private void Detach()
private static void OnMediaPlayerChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (!IsDesignMode)
if (e.OldValue is MediaPlayer oldMediaPlayer)
{
if (MediaPlayer != null)
{
MediaPlayer.Hwnd = IntPtr.Zero;
}
oldMediaPlayer.Hwnd = IntPtr.Zero;
}
if (e.NewValue is MediaPlayer newMediaPlayer)
{
newMediaPlayer.Hwnd = ((VideoView)d).Hwnd;
}
}
......@@ -56,25 +47,7 @@ namespace LibVLCSharp.WPF
private ForegroundWindow ForegroundWindow { get; set; }
private bool IsUpdatingContent { get; set; }
private UIElement ViewContent { get; set; }
private MediaPlayer _mediaPlayer;
public MediaPlayer MediaPlayer
{
get => _mediaPlayer;
set
{
if (_mediaPlayer != value)
{
Detach();
_mediaPlayer = value;
if (_mediaPlayer != null)
{
Attach();
}
}
}
}
private IntPtr Hwnd { get; set; }
public override void OnApplyTemplate()
{
......@@ -90,6 +63,21 @@ namespace LibVLCSharp.WPF
Content = ViewContent
};
}
Hwnd = (Template.FindName(PART_PlayerView, this) as System.Windows.Forms.Panel)?.Handle ?? IntPtr.Zero;
if (Hwnd == null)
{
Trace.WriteLine("HWND is NULL, aborting...");
return;
}
if (MediaPlayer == null)
{
Trace.Write("No MediaPlayer is set, aborting...");
return;
}
MediaPlayer.Hwnd = Hwnd;
}
}
......@@ -128,7 +116,10 @@ namespace LibVLCSharp.WPF
{
if (disposing)
{
Detach();
if(MediaPlayer != null)
{
MediaPlayer.Hwnd = IntPtr.Zero;
}
}
ViewContent = null;
......@@ -142,6 +133,7 @@ namespace LibVLCSharp.WPF
{
Dispose(true);
}
#endregion
}
}
\ No newline at end of file
......@@ -709,4 +709,16 @@ namespace LibVLCSharp.Shared
#endif
}
public class MediaPlayerChangedEventArgs : EventArgs
{
public MediaPlayerChangedEventArgs(LibVLCSharp.Shared.MediaPlayer oldMediaPlayer, LibVLCSharp.Shared.MediaPlayer newMediaPlayer)
{
OldMediaPlayer = oldMediaPlayer;
NewMediaPlayer = newMediaPlayer;
}
public LibVLCSharp.Shared.MediaPlayer OldMediaPlayer { get; }
public LibVLCSharp.Shared.MediaPlayer NewMediaPlayer { get; }
}
}
\ No newline at end of file
namespace LibVLCSharp.Shared
using System;
namespace LibVLCSharp.Shared
{
public interface IVideoView
{
......
using System.Diagnostics;
using LibVLCSharp.Shared;
using System.Diagnostics;
using Xamarin.Forms;
namespace LibVLCSharp.Forms.Sample
......@@ -29,7 +30,7 @@ namespace LibVLCSharp.Forms.Sample
Trace.WriteLine("MediaPlayer change raised from ViewModel.Propertychanged");
}
private void VideoView_MediaPlayerChanged(object sender, Shared.MediaPlayerChangedEventArgs e)
private void VideoView_MediaPlayerChanged(object sender, MediaPlayerChangedEventArgs e)
{
Trace.WriteLine("VideoView_MediaPlayerChanged");
}
......
......@@ -24,8 +24,6 @@ namespace LibVLCSharp.WPF.Sample
private void Controls_Loaded(object sender, RoutedEventArgs e)
{
Core.Initialize();
_libVLC = new LibVLC();
_mediaPlayer = new MediaPlayer(_libVLC);
......
......@@ -25,19 +25,12 @@ namespace LibVLCSharp.WPF.Sample
};
test.Children.Add(label);
Loaded += Controls_Loaded;
}
private void Controls_Loaded(object sender, RoutedEventArgs e)
{
Core.Initialize();
_libVLC = new LibVLC();
_mediaPlayer = new MediaPlayer(_libVLC);
VideoView.MediaPlayer = _mediaPlayer;
}
void StopButton_Click(object sender, RoutedEventArgs e)
{
if (VideoView.MediaPlayer.IsPlaying)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment