WPF窗口在顶部播放
本文关键字:播放 顶部 窗口 WPF | 更新日期: 2023-09-27 18:04:18
我想在播放(音频/视频)时将我的窗口顶部最大化
我想确定播放器是否正在显示
请帮帮我。
<MediaElement x:Name="player" />
Xaml:
<CheckBox x:Name="allwaystop" Content="Always" Checked="allwaystop_Checked" />
<CheckBox x:Name="whileplaying" Content="While Playing" Checked="whileplaying_Checked"/>
<CheckBox x:Name="nevertop" Content="Never" IsChecked="True" Checked="nevertop_Checked" />
Xaml.cs:
private void allwaystop_Checked(object sender, RoutedEventArgs e)
{
// Always top
Window parent = Window.GetWindow(this);
parent.Topmost = true;
}
private void whileplaying_Checked(object sender, RoutedEventArgs e)
{
//WhilePlaying
// What??
}
private void nevertop_Checked(object sender, RoutedEventArgs e)
{
//Never top
Window parent = Window.GetWindow(this);
parent.Topmost = false;
}
请帮帮我。
我将在while播放复选框代码中编写哪些代码?
首先,我认为单选按钮更适合这个目的。
第二,你很幸运,我在挖掘你的项目…这里有更多的免费代码…还有改进的空间,尽管它会让你继续前进。下面的代码实现了一个Player状态跟踪器,并使用该跟踪器进行各种操作。您将注意到跟踪器变量已被添加到您的许多方法中,因此Player状态可以相应地更新。这可能和@Bolu在回答你的问题时所想的很相似。<RadioButton x:Name="rdobtn_AlwaysTop" Content="Always top" HorizontalAlignment="Left" Margin="10,258,0,0" VerticalAlignment="Top" GroupName="WindowZIndex" Checked="SetWindowZIndex"/>
<RadioButton x:Name="rdobtn_TopWhilePlaying" Content="Top while playing" HorizontalAlignment="Left" Margin="96,258,0,0" VerticalAlignment="Top" GroupName="WindowZIndex" Checked="SetWindowZIndex"/>
<RadioButton x:Name="rdobtn_LetUserPick" Content="Who cares" HorizontalAlignment="Left" Margin="219,258,0,0" VerticalAlignment="Top" GroupName="WindowZIndex" IsChecked="True" Checked="SetWindowZIndex" />
public static bool IsMediaPlaying = false;//something to keep track of the player state
private void play_Click(object sender, RoutedEventArgs e)
{
PlayMedia();
}
public void PlayMedia()
{
if (IsMediaPlaying == false)
{
//nothing is playing
if (mePlayer.Source == new Uri(playlist[listbox4.SelectedIndex].Filepath, UriKind.RelativeOrAbsolute))
{
//nothing is playing, but the player already has the correct file loaded
mePlayer.LoadedBehavior = MediaState.Play;//play it
}
else
{
//nothing is playing and the media player's loaded file(if any) is different than the selected file so play the selected file
mePlayer.Source = new Uri(playlist[listbox4.SelectedIndex].Filepath, UriKind.RelativeOrAbsolute);
mePlayer.LoadedBehavior = MediaState.Play;
}
}
else//something is already playing
{
if (mePlayer.Source == new Uri(playlist[listbox4.SelectedIndex].Filepath, UriKind.RelativeOrAbsolute))
{
//the proper file is already playing
}
else
{
//something is playing but it looks like the user wants to play something different
mePlayer.Source = new Uri(playlist[listbox4.SelectedIndex].Filepath, UriKind.RelativeOrAbsolute);
mePlayer.LoadedBehavior = MediaState.Play;
}
}
IsMediaPlaying = true;
SetWindowZIndex();
}
private void SetWindowZIndex(object sender, RoutedEventArgs e)
{
SetWindowZIndex();
}
private void SetWindowZIndex()
{
if (rdobtn_AlwaysTop.IsChecked == true)
{
Window parent = Window.GetWindow(this);
parent.Topmost = true;
}
else if (rdobtn_TopWhilePlaying.IsChecked == true)
{
if (IsMediaPlaying == true)
{
Window parent = Window.GetWindow(this);
parent.Topmost = true;
}
else
{
Window parent = Window.GetWindow(this);
parent.Topmost = false;
}
}
else if (rdobtn_LetUserPick.IsChecked == true)
{
Window parent = Window.GetWindow(this);
parent.Topmost = false;
}
}
private void mePlayer_MediaFailed(object sender, ExceptionRoutedEventArgs e)
{
IsMediaPlaying = false;
RoutedEventArgs newEventArgs1 = new RoutedEventArgs(Button.ClickEvent); //Go to next
button1.RaiseEvent(newEventArgs1);
}
private void mePlayer_MediaEnded(object sender, RoutedEventArgs e)
{
IsMediaPlaying = false;
RoutedEventArgs newEventArgs = new RoutedEventArgs(Button.ClickEvent); //Go to next
button1.RaiseEvent(newEventArgs);
}
private void play_Click(object sender, RoutedEventArgs e)
{
PlayMedia();
}
private void pause_Click(object sender, RoutedEventArgs e)
{
mePlayer.LoadedBehavior = MediaState.Pause;
IsMediaPlaying = false;
}
private void listbox4_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
PlayMedia();
}
这有点困难,因为MediaElement
没有OnPlaying EventHandler或IsPlaying属性。
你可以这样做:
- 创建一个属性
IsMediaPlaying
,它将被您的Play
、Pause
和Stop
方法更新。 - 在
whileplaying_Checked
EventHandler中,检查IsMediaPlaying
,如果为真则设置Topmost=true
; - 在你的开始/播放方法中,检查
whileplaying.IsChecked
是否为真,如果为真,设置Topmost = ture
。SetIsMediaPlaying = true
- 同样在你的开始/播放方法中,订阅
MediaElement.MediaEnded
事件,也可能是MediaElement.MediaFailed
事件。并在EventHandler
中设置IsMediaPlaying=false
并检查whileplaying.IsChecked
是否为真,当为真时设置Topmost = false
。 - 在"
whileplaying_UnChecked
eventandler"中,根据其他选项设置Topmost
。(即如果IsMediaPlaying
,则设置Topmost=false
)