在WPF的ItemTemplate中添加MediaElement时出错

问题描述

我有一个应用程序,该应用程序从API检索所有视频以进行搜索查询,并认将它们显示为带有图像的网格。接收到的数据还包含视频的URL,因此当我将鼠标悬停在图像上时,我想播放视频。但是,当我为视频添加MediaElement时,它给了我错误“ NullReferenceException:对象引用未设置为对象的实例。”

我尝试使用 GetIsInDesignMode

禁止显示警告
 if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
            {
                //Code that throws the exception
            }

但这没有帮助。 window.xaml的代码

<Window x:Class="ResponsiveGrid.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:ResponsiveGrid"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <ListBox x:Name="listBox1" ScrollViewer.HorizontalScrollBarVisibility="disabled">
            <ListBox.ItemsPanel>
                <ItemsPanelTemplate>
                    <WrapPanel ItemHeight="250"
                               ItemWidth="250"
                               Orientation="Horizontal" />
                </ItemsPanelTemplate>
            </ListBox.ItemsPanel>
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid Width="250" Height="250">
                        <Grid.ColumnDeFinitions>
                            <ColumnDeFinition />
                            <ColumnDeFinition />
                            <ColumnDeFinition />
                        </Grid.ColumnDeFinitions>
                        <Grid.RowDeFinitions>
                            <RowDeFinition  />
                            <RowDeFinition />
                            <RowDeFinition />
                        </Grid.RowDeFinitions>
                        <Image  Source="" Grid.ColumnSpan="3" HorizontalAlignment="Left" Height="250" Grid.RowSpan="3" VerticalAlignment="Top" Width="250" Stretch="UniformToFill"/>
                        <MediaElement Source="" />

                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
        
    </Grid>
</Window>
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 ResponsiveGrid
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
            {
                //Code that throws the exception
            }
            listBox1.ItemsSource = Enumerable.Range(1,100);
        }
    }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)