我想將 MediaPlayer 用于帶有 MediaElement 的視頻和圖片。我已經做過測試,MediaElement也可以顯示圖片。目前我的問題是 MediaElement 似乎打開了兩次。這是示例代碼:XAML:<Window x:Class="TestMediaElement.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:TestMediaElement" mc:Ignorable="d" Title="MainWindow" Height="450" Width="800"><Grid Margin="10"> <MediaElement Name="mediaPlayer" MediaOpened="media_MediaOpened" LoadedBehavior="Play" UnloadedBehavior="Manual"/></Grid></Window>C#using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows;using System.Windows.Controls;using System.Windows.Data;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Imaging;using System.Windows.Navigation;using System.Windows.Shapes;namespace TestMediaElement{public partial class MainWindow : Window{ int currentMediaIndex = 0; string[] Documents; public MainWindow() { InitializeComponent(); Documents = System.IO.Directory.GetFiles("C:/Users/Feller/Desktop/Test/"); Uri first = new Uri(Documents[0], UriKind.RelativeOrAbsolute); mediaPlayer.Source = first; mediaPlayer.MediaOpened += media_MediaOpened; } private void media_MediaOpened(object sender, RoutedEventArgs e) { Console.WriteLine("Video opened"); } }}另一個問題是圖像會在大約 5 秒后自動關閉。誰能幫我解決這些問題?非常感謝!
1 回答
浮云間
TA貢獻1829條經驗 獲得超4個贊
MediaOpened 事件被訂閱了兩次。從 xaml 中刪除或從代碼中刪除。
//mediaPlayer.MediaOpened += media_MediaOpened;
- 1 回答
- 0 關注
- 110 瀏覽
添加回答
舉報
0/150
提交
取消
