WPF MediaElement:视频打开了两次

WPF MediaElement: Video opened twice

我想将 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 秒后自动关闭。 谁能帮我解决这些问题? 非常感谢!

MediaOpened 事件被订阅了两次。从 xaml 中删除或从代码中删除。

    //mediaPlayer.MediaOpened += media_MediaOpened;