前台:
<Grid x:Name="main"> <Grid.ColumnDefinitions> <ColumnDefinition Width="*"/> <ColumnDefinition Width="*"/> <ColumnDefinition Width="*"/> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="*"/> <RowDefinition Height="*"/> </Grid.RowDefinitions> <MediaElement x:Name="me1" Grid.Row="0" Grid.Column="0" /> <MediaElement x:Name="me2" Grid.Row="0" Grid.Column="1" /> <MediaElement x:Name="me3" Grid.Row="0" Grid.Column="2" /> <MediaElement x:Name="me4" Grid.Row="1" Grid.Column="0" /> <MediaElement x:Name="me5" Grid.Row="1" Grid.Column="1" /> <MediaElement x:Name="me6" Grid.Row="1" Grid.Column="2" /> </Grid>
后台:
private void Window_Loaded(object sender, RoutedEventArgs e) { var action = new Action(() => { var suffix = "wmv"; var index = 1; foreach (var child in this.main.Children) { if (child is MediaElement) { var mediaElement = child as MediaElement; var uriString = string.Format(@"C:\Users\Public\Videos\Sample Videos\{0}.{1}", index, suffix); var uri = new Uri(uriString); mediaElement.Source = uri; index++; } } }); this.Dispatcher.BeginInvoke(action); }
运行后,不管如何都会少播放一个视频。
怕是Sample Videos文件夹下面文件名不是1.wmv,2.wmv,3.wmv,4.wmv,5.wmv,6.wmv
index是5么
我做过测试。
你可以把代码拷贝进项目运行一下你就明白了。
@Me_Code: 是六次啊