使用<MediaElement>
在网格中显示视频时遇到问题。我已经创建了一个单独的类来获取具有get set属性的视频详细信息。问题是我在调试程序时无法在窗口加载时访问视频文件夹。我也在debug文件夹中保存了一个名为Videos的文件夹。如果有人可以帮忙!!提前谢谢。
VideoDetail.cs
public string Name { get; set; }
public string Description { get; set; }
public string Path { get; set; }
public string FileName { get; set; }
public string Extension { get; set; }
public int Height { get; set; }
public int Width { get; set; }
public long Size { get; set; }
Window.xaml
<ItemsControl Name="VideoList" ItemsSource="{Binding VideoList}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border BorderThickness="1" BorderBrush="#FFD0D1D7" Padding="5" Margin="10,10,0,0">
<StackPanel Orientation="Horizontal">
<!--image -->
<Grid Width="200" Height="150">
<MediaElement Source="{Binding Path}"/>
</Grid>
</StackPanel>
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
Window.xaml.cs
private void Window_Loaded(object sender, RoutedEventArgs e)
{
string root = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
string[] supportedExtensions = new[] { ".mp4" };
var files = Directory.GetFiles(Path.Combine(root, "Videos"), "*.*").Where(s => supportedExtensions.Contains(Path.GetExtension(s).ToLower()));
List<ImageDetails> videos = new List<ImageDetails>();
foreach (var file in files)
{
ImageDetails id = new ImageDetails()
{
Path = file,
FileName = Path.GetFileName(file),
Extension = Path.GetExtension(file)
};
FileInfo fi = new FileInfo(file);
id.Size = fi.Length;
videos.Add(id);
}
VideoList.ItemsSource = videos;
}
}