Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# MediaElement控件不显示视频,仅在网格应用程序中播放视频的音频_C#_Xaml_Windows 8_Windows Store Apps - Fatal编程技术网

C# MediaElement控件不显示视频,仅在网格应用程序中播放视频的音频

C# MediaElement控件不显示视频,仅在网格应用程序中播放视频的音频,c#,xaml,windows-8,windows-store-apps,C#,Xaml,Windows 8,Windows Store Apps,我在windows 8应用商店应用程序的GridApp的itemsdetail页面的flipview中使用MediaElement控件,我在使用它时遇到了一种特殊类型的问题。MediaElement不显示视频,在预期的视频输出上只有一个黑屏,但它可以很好地播放视频的音频 现在我的问题的特殊部分是,在grid应用程序中,一个组中有项目集合(我知道每个人都可能知道这一点,只是为了让自己清楚),每个组的第一个项目播放视频很好,我的意思是它显示视频和音频很好,但是组中的其他项目不显示视频,只播放视频的音

我在windows 8应用商店应用程序的GridApp的itemsdetail页面的flipview中使用MediaElement控件,我在使用它时遇到了一种特殊类型的问题。MediaElement不显示视频,在预期的视频输出上只有一个黑屏,但它可以很好地播放视频的音频

现在我的问题的特殊部分是,在grid应用程序中,一个组中有项目集合(我知道每个人都可能知道这一点,只是为了让自己清楚),每个组的第一个项目播放视频很好,我的意思是它显示视频和音频很好,但是组中的其他项目不显示视频,只播放视频的音频。有人知道为什么会这样吗

以下是XAML代码:

 <FlipView
x:Name="flipView"
AutomationProperties.AutomationId="ItemsFlipView"
AutomationProperties.Name="Item Details"
TabIndex="1"
Grid.RowSpan="2"
ItemsSource="{Binding Source={StaticResource itemsViewSource}}">

<FlipView.ItemContainerStyle>
    <Style TargetType="FlipViewItem">
        <Setter Property="Margin" Value="0,137,0,0"/>
    </Style>
</FlipView.ItemContainerStyle>

<FlipView.ItemTemplate>
    <DataTemplate>
        <UserControl Loaded="StartLayoutUpdates" Unloaded="StopLayoutUpdates">
            <ScrollViewer x:Name="scrollViewer" Style="{StaticResource VerticalScrollViewerStyle}" Grid.Row="1">
                <Grid Margin="120,0,20,20">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="400" />
                        <ColumnDefinition Width="40" />
                        <ColumnDefinition Width="360" />
                        <ColumnDefinition Width="40" />
                        <ColumnDefinition />
                    </Grid.ColumnDefinitions>
                    <Border BorderBrush="Black" BorderThickness="1" Width="350" HorizontalAlignment="Left" Grid.Row="0">
                        <MediaElement x:Name="VideoSource" AutomationProperties.Name="VideoSource" Source="/Assets/Big_Buck_Bunny.mp4" HorizontalAlignment="Center" VerticalAlignment="Stretch" Height="250" Width="350" AutoPlay="False" IsLooping="True" />
                    </Border>
                    <Border BorderBrush="Black" BorderThickness="1" Height="65" Width="350" HorizontalAlignment="Left" Grid.Row="1">
                        <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
                            <Button x:Name="playButton" Margin="0,0" Click="playButton_Click" Style="{StaticResource PlayAppBarButtonStyle}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                            <Button x:Name="pauseButton" Margin="0,0" Click="pauseButton_Click" Style="{StaticResource PauseAppBarButtonStyle}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                        </StackPanel>
                    </Border>
                </Grid>
            </ScrollViewer>
        </UserControl>
    </DataTemplate>
</FlipView.ItemTemplate>

下面是隐藏的代码:

    private void playButton_Click(object sender, RoutedEventArgs e)
    {
        MediaElement media = FindControl<MediaElement>(this, "VideoSource") as MediaElement;
        media.Play();
    }

    private void pauseButton_Click(object sender, RoutedEventArgs e)
    {
        MediaElement media = FindControl<MediaElement>(this, "VideoSource") as MediaElement;
        media.Pause();
    }

    private void stopButton_Click(object sender, RoutedEventArgs e)
    {
        MediaElement media = FindControl<MediaElement>(this, "VideoSource") as MediaElement;
        media.Stop();
    }

    private DependencyObject FindControl<T>(DependencyObject controlType, string ctrlName)
    {
        int childNumber = VisualTreeHelper.GetChildrenCount(controlType);
        for (int i = 0; i < childNumber; i++)
        {
            DependencyObject child = VisualTreeHelper.GetChild(controlType, i);
            FrameworkElement fe = child as FrameworkElement;
            // Not a framework element or is null
            if (fe == null) return null;

            if (child is T && fe.Name == ctrlName)
            {
                // Found the control so return
                return child;
            }
            else
            {
                // Not found it - search children
                DependencyObject nextLevel = FindControl<T>(child, ctrlName);
                if (nextLevel != null)
                    return nextLevel;
            }
        }
        return null;
    }
private void playButton\u单击(对象发送者,路由目标)
{
MediaElement media=FindControl(此“视频源”)作为MediaElement;
媒体播放();
}
私有无效暂停按钮单击(对象发送器,路由目标)
{
MediaElement media=FindControl(此“视频源”)作为MediaElement;
媒体暂停();
}
私有无效停止按钮\单击(对象发送者,路由目标)
{
MediaElement media=FindControl(此“视频源”)作为MediaElement;
media.Stop();
}
私有DependencyObject FindControl(DependencyObject控件类型,字符串ctrlName)
{
int childNumber=VisualTreeHelper.GetChildrenCount(controlType);
for(int i=0;i

我希望我能把这个问题讲清楚。

所以,你所尝试的方法会奏效。不过,为了有所帮助,我编写了一个小型原型

使用以下代码:

public sealed partial class MainPage : Page
{
    public MainPage()
    {
        this.InitializeComponent();
    }

    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        // pause
        var _Media = GetMediaElement(sender as Button);
        _Media.Pause();
    }

    private void Button_Click_2(object sender, RoutedEventArgs e)
    {
        // play
        var _Media = GetMediaElement(sender as Button);
        _Media.Play();
    }

    MediaElement GetMediaElement(Button button)
    {
        var _Parent = button.Parent as Grid;
        var _GrandParent = _Parent.Parent as Grid;
        return _GrandParent.Children.First() as MediaElement;
    }

    private void FlipView_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
    {
        var _FlipView = sender as FlipView;
        foreach (var item in _FlipView.Items)
        {
            var _Container = _FlipView.ItemContainerGenerator.ContainerFromItem(item);
            var _Children = AllChildren(_Container);
            foreach (var media in _Children)
                media.Pause();
        }
    }

    public List<MediaElement> AllChildren(DependencyObject parent)
    {
        if (parent == null)
            return (new MediaElement[] { }).ToList();
        var _List = new List<MediaElement> { };
        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
        {
            var _Child = VisualTreeHelper.GetChild(parent, i);
            if (_Child is MediaElement)
                _List.Add(_Child as MediaElement);
            _List.AddRange(AllChildren(_Child));
        }
        return _List;
    }
}
公共密封部分类主页面:第页
{
公共主页()
{
this.InitializeComponent();
}
私有无效按钮\u单击\u 1(对象发送者,路由目标)
{
//停顿
var\u Media=GetMediaElement(发送方作为按钮);
_媒体暂停();
}
私有无效按钮\u单击\u 2(对象发送方,路由目标)
{
//玩
var\u Media=GetMediaElement(发送方作为按钮);
_媒体播放();
}
MediaElement获取MediaElement(按钮)
{
var\u Parent=button.Parent作为网格;
var\u祖父母=\u Parent.Parent作为网格;
返回_祖父母.Children.First()作为MediaElement;
}
private void FlipView_SelectionChanged_1(对象发送者,SelectionChangedEventArgs e)
{
var _FlipView=发送方作为FlipView;
foreach(FlipView.Items中的变量项)
{
var\u Container=\u FlipView.ItemContainerGenerator.ContainerFromItem(item);
var _Children=AllChildren(_容器);
foreach(子系统中的var媒体)
媒体暂停();
}
}
公共列表所有子项(DependencyObject父项)
{
如果(父项==null)
return(新的MediaElement[]{}).ToList();
var_List=新列表{};
for(int i=0;i
并使用这个XAML:

<FlipView SelectionChanged="FlipView_SelectionChanged_1">
    <FlipView.ItemTemplate>
        <DataTemplate>
            <Grid>
                <MediaElement Source="{Binding}" AutoPlay="False" />
                <Grid Margin="20" VerticalAlignment="Bottom" Background="Black">
                    <Button Click="Button_Click_1" HorizontalAlignment="Left">Pause</Button>
                    <Button Click="Button_Click_2" HorizontalAlignment="Right">Play</Button>
                </Grid>
            </Grid>
        </DataTemplate>
    </FlipView.ItemTemplate>
    <x:String>ms-appx:/Assets/BestSingingVideoEver.wmv</x:String>
    <x:String>ms-appx:/Assets/BestSingingVideoEver.wmv</x:String>
    <x:String>ms-appx:/Assets/BestSingingVideoEver.wmv</x:String>
    <x:String>ms-appx:/Assets/BestSingingVideoEver.wmv</x:String>
</FlipView>

暂停
玩
ms appx:/Assets/BestSingingVideoEver.wmv
ms appx:/Assets/BestSingingVideoEver.wmv
ms appx:/Assets/BestSingingVideoEver.wmv
ms appx:/Assets/BestSingingVideoEver.wmv
请确保使用您自己的视频更新路径。当然,您也可以对列表进行数据绑定。我的代码只是一个原型,用来证明你想要做的是可能的。祝你好运


我还认为我可能已经解决了代码中的一两个bug。特别是在列表中查找子控件的部分。以下是我的参考资料:

视频是否在其他应用程序中播放?试试看。@Matt Harrington我不知道你在问什么,如果你问mediaelement是否支持这种视频格式,那么是的,它确实会播放这种视频格式。Jerry感谢我对你给出的原型的回复,如果有任何问题,我会告诉你。同时我在想,该页面中的所有其他元素都从数据绑定接收数据,这可能是因为我没有绑定我面临问题的媒体元素的源路径吗?感谢您的帮助,您的原型代码