WPF - MediaElement 显示为空

问题描述 投票:0回答:1

我正在编写一个小应用程序来播放/暂停和停止 WPF 中的视频。

一切都很棒,但是当我想添加手动控件并检索视频时,它返回一个空值,当视频清楚地存在并且它不为空时;或者无论如何都不应该!

xaml文件的代码是:

<Window x:Class="WpfApp1.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:WpfApp1"
        mc:Ignorable="d"
        Title="MainWindow" Height="600px" Width="800px">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="Auto"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <MediaElement
            x:Name="mediaElement"
            
            Grid.ColumnSpan="3"
            Grid.Row="0"
            Height="400px"
            LoadedBehavior="Manual"
            Source="C:\Users\User\Videos\the_video_00.mp4"
            UnloadedBehavior="Stop"
            Width="700px"
        />
        <Separator
            Background="Transparent"
            Grid.ColumnSpan="3"
            Grid.Row="1"
            Height="10px"
        />
        <Button
            x:Name="buttonPlay"
            
            Grid.Column="0"
            Grid.Row="2"
            HorizontalAlignment="Center"
            Height="30px"
            Width="75px"
            Click="PlayButton_Click"
        >Play</Button>
        <Separator
            Background="Transparent"
            Grid.Column="1"
            Grid.Row="2"
            Width="490px"
        />
        <Button
            x:Name="buttonStop"

            Grid.Column="2"
            Grid.Row="2"
            HorizontalAlignment="Center"
            Height="30px"
            Width="75px"
            Click="StopButton_Click"
        >Stop</Button>
    </Grid>
</Window>

C# 文件的代码是:

using System.Numerics;
using System.Text;
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 WpfApp1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {

        // ---------------------------------------------------------------------
        // Variables
        // ---------------------------------------------------------------------

        private bool is_playing = false;

        // ---------------------------------------------------------------------
        // Main Window
        // ---------------------------------------------------------------------

        public MainWindow()
        {
            InitializeComponent();
        }

        // ---------------------------------------------------------------------
        // Event Listeneres
        // ---------------------------------------------------------------------

        private void PlayButton_Click(object sender, RoutedEventArgs e)
        {
            // Get the video player.
            var player = this.FindName("mediaElement") as MediaElement;

            // No video player found.
            if (player == null) { MessageBox.Show("Player is null!"); return; }
            

            // Play/Pause the video.
            is_playing = !is_playing;

            if (is_playing)
            {   
                player.Play();
                return;
            }

            player.Pause();
        }

        private void StopButton_Click(object sender, RoutedEventArgs e)
        {
            // Get the video player.
            var player = this.FindName("mediaElement") as MediaPlayer;

            // No video player found.
            if (player == null) return;

            // Play/Pause the video.
            is_playing = false;
            player.Close();
        }
    }
}

问题是找不到

"mediaElement"
这个名称,因此无法控制视频。我非常确定该视频确实存在,因为当设置属性
LoadedBehavior="Play"
时,它确实会播放!

问题

如何让它发挥作用?

出于某种奇怪的原因它以前有效,但现在不行了!

谢谢!

c# wpf mediaelement
1个回答
0
投票

在 StopButton_Click 中是“作为 MediaPlayer”。将其更改为“as MediaElement”,就像 PlayButton_Click 中一样。那么你的例子就会很有魅力! :-)

© www.soinside.com 2019 - 2024. All rights reserved.