如何在Visual Studio Designer中使用DataTemplate中的MediaElement?

时间:2017-07-23 21:36:22

标签: wpf xaml datatemplate mediaelement

WPF .Net 4.6

  

对象引用未设置为对象的实例

Visual Studio 2015 Community Edition使用上述警告强调DataTemplate中的“MediaElement”。在发布或调试模式下运行XAML似乎没有问题,但蓝色下划线让我发疯!

我该如何解决这个问题?

TIA

仅供参考,ItemsControl绑定到代码隐藏中的复合集合:

C#:

            var d1 = new ObservableCollection<ImageInfo>()
                  {
                    new ImageInfo(){Height=50, Width=double.NaN, Uri= new Uri("pack://application:,,,/Images/Skeleton.png")},
                    new ImageInfo(){Height=50, Width=double.NaN, Uri= new Uri("pack://application:,,,/Images/Female.jpg")},
                    new ImageInfo(){Height=50, Width=double.NaN, Uri= new Uri("pack://application:,,,/Images/Male.jpg")},
                    new ImageInfo(){Height=50, Width=double.NaN, Uri= new Uri("pack://application:,,,/Images/Skeletal Anatomy - Anterior View.png")},
                    new ImageInfo(){Height=50, Width=double.NaN, Uri= new Uri("pack://application:,,,/Images/Male - Front - Nude.png")},
                    new ImageInfo(){Height=50, Width=double.NaN, Uri= new Uri("pack://application:,,,/Images/Male - Back.png")},
                    new ImageInfo(){Height=50, Width=double.NaN, Uri= new Uri("pack://application:,,,/Images/Female - Front.png")},
                    new ImageInfo(){Height=50, Width=double.NaN, Uri= new Uri("pack://application:,,,/Images/Female - Back.png")}
                  };

            var d2 = new ObservableCollection<MovieInfo>()
            {
               new MovieInfo() {Uri = new Uri(@"C:\Nova6\Client\Doctor_Desk\Images\TribeWithFire.wmv", UriKind.Absolute) }
            };

            CompositeCollection data = new CompositeCollection();
            data.Add(new CollectionContainer() { Collection = d1 });
            data.Add(new CollectionContainer() { Collection = d2 });

            ImageCanvasMenu.ItemsSource = data;

XAML:

                        <ItemsControl 
                              Loaded="ImageCanvasMenu_Loaded"  
                              HorizontalAlignment="Left"
                              VerticalAlignment="Top" 
                              x:Name="ImageCanvasMenu" Background="LightCyan"  >

                        <ItemsControl.ItemsPanel>
                            <ItemsPanelTemplate>
                                  <panels:CustomPanel />
                            </ItemsPanelTemplate>
                        </ItemsControl.ItemsPanel>

                        <ItemsControl.Resources>
                            <DataTemplate DataType="{x:Type ds:ImageInfo}">
                                <Image Height="60" Margin="0,20,0,0">
                                    <Image.Source >
                                        <BitmapImage UriSource="{Binding Uri}"/>
                                    </Image.Source>
                                </Image>
                            </DataTemplate>
                            <DataTemplate DataType="{x:Type ds:MovieInfo}">
                                <ContentControl Height="60" Margin="0,20,0,0">
                                    <MediaElement Source="{Binding Uri}" MediaFailed="MediaElement_MediaFailed"
                                     LoadedBehavior="Manual" 
                                     ScrubbingEnabled="True"  
                                     Loaded="Video1_Loaded"             
                                     IsMuted="True"
                                     MediaEnded="MediaElement_MediaEnded"
                                     HorizontalAlignment="Left"
                                     Stretch="UniformToFill"
                                     />
                                </ContentControl>
                            </DataTemplate>
                        </ItemsControl.Resources>
                        <ItemsControl.ItemContainerStyle>
                            <Style TargetType="ContentPresenter">
                                <EventSetter Event="PreviewMouseDown" Handler="MediaCenter_PreviewMouseDown"/>
                            </Style>
                        </ItemsControl.ItemContainerStyle>
                    </ItemsControl>

0 个答案:

没有答案