0

我有 TabControl,我希望 TabItems 的内容中有视频。我有将视频添加到选项卡内容的方法:

private void AddVideosFromListToTab(IEnumerable<VideoClass> list, TabItem tab)
    {
        var stackPanel = new StackPanel { Orientation = Orientation.Horizontal };
        foreach (
            var media in
                list.Select(
                    video =>
                    MediaUriElementHelper.CreatePreviewMediaElementUri(video.SamplePath(), 200, 150, mediaPreviewControls,
                                                                       mediaPlayer_MouseDown)))
        {
            stackPanel.Children.Add(media);
        }
        stackPanel.Children.Add(new TextBlock {Text = "Some text"});
        tab.Content = stackPanel;
    }

这是我用视频创建媒体的方法。我正在使用 MediaUriElement,因为它的性能更好。

    public static MediaUriElement CreatePreviewMediaElementUri(string sourceFile, int width, int height, List<MediaUriElement> listMedias, System.Windows.Input.MouseButtonEventHandler mouseDown)
    {
        var media = new MediaUriElement();
        media.BeginInit();
        media.Width = width;
        media.Height = height;
        media.Margin = new Thickness(20);
        media.Volume = 0;
        media.LoadedBehavior = WPFMediaKit.DirectShow.MediaPlayers.MediaState.Play;
        media.MouseDown += mouseDown;
        media.Loop = true;
        media.Source = new Uri(sourceFile);
        media.EndInit();
        listMedias.Add(media);
        return media;
    }

当应用程序加载时没关系,但是当我选择另一个选项卡时没有视频,当我返回第一个选项卡时也没有视频了。我添加了一些文本,并且一直存在。所以问题出在 MediaUriElement 上。有人知道如何解决这个问题吗?谢谢

4

2 回答 2

0

我想因为普通的 TabConrol 只有一个 contentPresenter 它会丢弃以前的内容。如果是这样,这应该有效: Stop TabControl from recreate its children

于 2013-03-13T11:47:42.093 回答
0

我设法在不使用 TabControlEx 的情况下修复了它(因为它也对我不起作用)。我使用 Datatemplates 和 templateselector 在选项卡之间切换。

xml:

 <Window.Resources>
    <local:MainTabViewDataTemplateSelector x:Key="myMainContentTemplateSelector" />
    <DataTemplate x:Key="Dashboard">
        <views:DashboardView />
    </DataTemplate>
    <DataTemplate x:Key="SystemHealth">
        <views:SystemHealthView />
    </DataTemplate>
</Window.Resources>
        <TabControl ItemsSource="{Binding MainTabs}"
                Margin="0,33,0,0"
                Grid.RowSpan="2"
                SelectedIndex="0"
                 Width="auto" 
                Style="{DynamicResource TabControlStyleMain}"
                ContentTemplateSelector="{StaticResource myMainContentTemplateSelector}"
                Padding="20" Grid.ColumnSpan="2"
                VerticalAlignment="Stretch">
        <TabControl.Background>

            <ImageBrush ImageSource="/SystemHealthAndDashboard;component/Images/innerBackground.png"/>

            </TabControl.Background>
        <TabControl.ItemTemplate>
            <DataTemplate >
                    <TextBlock Grid.Column="0" Text="{Binding Name}" VerticalAlignment="Center" HorizontalAlignment="Left"/>
            </DataTemplate>
        </TabControl.ItemTemplate>
    </TabControl>

数据模板选择器:

 public class MainTabViewDataTemplateSelector : DataTemplateSelector
{
    public override DataTemplate SelectTemplate(object item, DependencyObject container)
    {
        FrameworkElement element = container as FrameworkElement;
        switch ((item as TabInfoEntity).TabIndex)
        {
            case 1:
                {
                    return element.FindResource("Dashboard") as DataTemplate;
                }
            case 2:
                {
                    return element.FindResource("SystemHealth") as DataTemplate;
                }

        }
        return null;
    }
}

TabInfoEntity 类(这种类型的对象列表是 TabControl 的 itemsource):

公共类 TabInfoEntity { 公共 TabInfoEntity() {

    }
    private string name;

    public string Name
    {
        get { return name; }
        set { name = value; }
    }

    private int tabindex;

    public int TabIndex
    {
        get { return tabindex; }
        set { tabindex = value; }
    }
}
于 2013-05-01T09:03:29.437 回答