I have an application that gets all videos from an API for a search query and shows them as a grid with image by default. The received data also contains the URL for the video so I want to play the video when I hover over the image. But when I added the MediaElement for the video it gives me the error "NullReferenceException: Object reference not set to an instance of an object."
I have tried suppressing the warning using GetIsInDesignMode
if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
{
//Code that throws the exception
}
But it doesn't help. The code for window.xaml is
<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);
}
}
}