我正在制作视频播放器。在应用程序中,我有一个全屏窗口和一个普通窗口。
我正在使用ContentControl
从我的ViewModel中绑定MediaElement
。我的MediaElement
实例是在ViewModel中创建的,因此通过代码更容易控制此控件。
现在我有一个Button
来显示另一个ContentControl
窗口,我可以在其中指定同一个MediaElement
。有用。元素从我的普通窗口切换到我的全屏。
问题是,当我关闭全屏窗口时,它不会切换回我的正常窗口ContentControl
。
我尝试重新绑定控件,它可以在控件内部看到控件,但对任何命令都不起作用。
FullScreenView
<Grid>
<ContentControl Content="{Binding PlayerBaseViewModel.MediaControl, Mode=OneWay}" />
</Grid>
PlayerBaseView
<Grid>
<ContentControl Content="{Binding MediaControl}" />
</Grid>
致电全屏
internal void ShowFullScreen(PlayerBaseViewModel playerBaseViewModel)
{
var fullScreenView = new FullScreenView();
var fullScreenViewModel = new FullScreenViewModel(
playerBaseViewModel,
this.PlayControlViewModel,
this.source);
fullScreenView.DataContext = fullScreenViewModel;
fullScreenView.ShowDialog();
fullScreenViewModel.Dispose();
}
基地
public FrameworkElement MediaControl
{
get
{
return this.mediaControl;
}
set
{
this.mediaControl = value;
if (this.mediaControl != null)
{
this.mediaControl.HorizontalAlignment = HorizontalAlignment.Center;
}
this.RaisePropertyChanged(() => this.MediaControl);
}
}
private void FullScreen()
{
if (this.ShowFullScreen != null)
{
MessageHelper.ShowMessage(
string.Format(StaticData.MessagePlayerBaseViewModelFullScreen, this.Device.Name),
string.Format(StaticData.MessagePlayerBaseViewModelFullScreenDetails, this.Device.Name),
3);
var control = this.mediaControl;
this.ShowFullScreen(this);
this.MediaControl = null;
control.DataContext = this;
this.MediaControl = control;
this.Device.ResetAllDevice();
}
}
PlayerAudioVideoViewModel
public class PlayerAudioVideoViewModel : PlayerBaseViewModel
{
#region Static Fields
private static readonly Stopwatch Watch = Stopwatch.StartNew();
#endregion
#region Fields
private readonly DependencyPropertyDescriptor mediaPositionDependencyPropertyDescriptor =
DependencyPropertyDescriptor.FromProperty(MediaSeekingElement.MediaPositionProperty, typeof(UIElement));
private readonly DispatcherTimer timer = new DispatcherTimer(DispatcherPriority.Render);
private MediaElement mediaElement = new MediaElement();
#endregion
#region Constructors and Destructors
public PlayerAudioVideoViewModel(DeviceModel deviceModel)
: base(deviceModel)
{
this.MediaControl = this.mediaElement;
this.mediaElement.BeginInit();
this.timer.Interval = TimeSpan.FromMilliseconds(200);
this.timer.Tick += this.TimerTick;
this.mediaElement.LoadedBehavior = MediaState.Manual;
this.mediaElement.ScrubbingEnabled = true;
this.mediaElement.MediaOpened += this.MediaOpened;
this.mediaElement.MediaEnded += this.MediaEnded;
this.mediaPositionDependencyPropertyDescriptor.AddValueChanged(this.mediaElement, this.PositionChanged);
this.mediaElement.EndInit();
}
#endregion
#region Public Methods and Operators
public override void CleanUp()
{
this.MediaControl = null;
this.mediaElement.Source = null;
this.mediaElement.MediaOpened -= this.MediaOpened;
this.mediaElement.MediaEnded -= this.MediaEnded;
this.mediaElement.LoadedBehavior = MediaState.Manual;
this.mediaElement.UnloadedBehavior = MediaState.Manual;
this.timer.Tick -= this.TimerTick;
this.mediaElement.Source = null;
this.mediaElement = null;
}
public override void LoadData()
{
if (this.Data != null)
{
var cont = (ContentMoving)this.Data.Content;
if (this.mediaElement.Source == null || !this.mediaElement.Source.LocalPath.Equals(cont.Path.FullName))
{
this.mediaElement.Source = new Uri(cont.Path.FullName, UriKind.Absolute);
this.mediaElement.Pause();
}
else
{
this.mediaElement.Position =
cont.Position.Subtract(new TimeSpan(0, 0, 0, 0, cont.Position.Milliseconds));
// this.ChangePlaySpeed();
this.timer.Start();
}
}
else
{
this.mediaElement.Source = null;
}
Debug.Print("D {0} Load.", this.DeviceName);
}
public override void MuteAudio(bool mute)
{
this.mediaElement.IsMuted = mute;
}
public override void Pause()
{
Debug.Print("D {0} Pause.", this.DeviceName);
this.mediaElement.Pause();
}
public override void Play()
{
Debug.Print("D {0} Play.", this.DeviceName);
this.mediaElement.Play();
}
#endregion
#region Methods
private void MediaEnded(object sender, RoutedEventArgs e)
{
if (this.mediaElement.Position == this.mediaElement.NaturalDuration)
{
this.DataEndReached();
}
}
private void MediaOpened(object sender, RoutedEventArgs e)
{
if (this.Data != null)
{
var cont = (ContentMoving)this.Data.Content;
if (this.Device.Status == PlayStatus.Play)
{
this.Device.ResetAllDevice();
}
else
{
this.mediaElement.Position =
cont.Position.Subtract(new TimeSpan(0, 0, 0, 0, cont.Position.Milliseconds));
this.ChangePlaySpeed();
}
}
this.DataLoaded();
}
#endregion
}
}
答案 0 :(得分:2)
原始窗口丢失视频的原因是因为当您关闭全屏窗口时; MediaElement
正在卸载,因为它是该窗口的一部分。要解决此问题,您需要先将MediaElement
加载/卸载行为更改为手动。默认情况下,它处于自动模式。这就是Media加载源后立即开始播放的原因。这也意味着当从UI树触发Unloaded事件时,MediaElement
将关闭。
将行为设置为手动后,您仍然需要处理全屏窗口关闭事件,以便您可以重新绑定主要内容中的ContentControl
。Content
(MediaElement
)窗口。这将触发&#34; Loaded&#34;再次MediaElement
中的事件,然后允许MediaElement
在主窗口中继续播放。
以下是示例代码;很直接。我正在展示ViewModel构造函数和相关方法。
ViewModel.cs的部分内容:
public RelayCommand Maximize { get; set; }
public RelayCommand Play { get; set; }
private MediaElement media;
// Bound media property with notification
public MediaElement Media
{
get
{
return this.media;
}
set
{
this.media = value;
// Modify following line as needed
// to any custom Property changed handler
this.Changed("Media");
}
}
// Constructor, setup the command, media element etc.
public ViewModel()
{
// Two button commands
this.Maximize = new RelayCommand(this.ExecuteMaximizeCommand);
this.Play = new RelayCommand(this.ExecutePlayCommand);
// The Media element
this.Media = new MediaElement();
// This is where we need to set Loaded and Unloaded behavior to Manual
// This means video won't play right-away after loading
// You will have to call .Play() method to start playback
// Add a play button if you don't have one and bind the Play command
this.Media.LoadedBehavior = MediaState.Manual;
this.Media.UnloadedBehavior = MediaState.Manual;
// Following is just an example. Doesn't need to be here
this.Media.Source = new Uri("test.mp4", UriKind.Relative);
}
public void ExecuteMaximizeCommand()
{
var fullscreen = new FullScreen();
// We need to handle the Closing event
// So that we can trigger the playback
// to continue in the smaller window
fullscreen.Closing += fullscreen_Closing;
fullscreen.DataContext = this;
fullscreen.ShowDialog();
}
public void ExecutePlayCommand()
{
this.Media.Play();
}
// Event handler for fullscreen window closing
void fullscreen_Closing(object sender, CancelEventArgs e)
{
// Trigger the Property Change Notification
// by forcing null and then back to original value
// This will trigger the binding to kick-in again
// which will redraw the MediaElement UI and continue
// the playback
var temp = this.Media;
this.Media = null;
this.Media = temp;
}
MainWindow.xaml
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="30" />
</Grid.RowDefinitions>
<ContentControl Content="{Binding Media}" />
<StackPanel Grid.Row="1" Orientation="Horizontal">
<Button Command="{Binding Maximize}" Content="Maximize" Margin="2" />
<Button Command="{Binding Play}" Content="Play" Margin="2" />
</StackPanel>
</Grid>
</Window>
<强> MainWindow.cs 强>
public MainWindow()
{
InitializeComponent();
this.DataContext = new ViewModel();
}
<强> FullScreen.xaml 强>
<Window x:Class="WpfApplication1.FullScreen"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="FullScreen" Height="300" Width="300" WindowState="Maximized">
<Grid>
<ContentControl x:Name="MediaContentControl" Content="{Binding Path=Media}">
</ContentControl>
</Grid>
</Window>