Пример #1
0
        /// <summary>
        /// Initializes the component.
        /// </summary>
        private void InitializeComponent()
        {
            // Synchronize initial property values to the MediaElement properties.
            // This is because the hosted element gets created after the MediaElement properties
            // might have been set.
            VideoView.ElementLoaded += (vs, ve) =>
            {
                VideoView.UseLayoutRounding   = true;
                VideoView.SnapsToDevicePixels = true;
                VideoView.Focusable           = false;
                VideoView.IsHitTestVisible    = false;
                VideoView.Stretch             = Stretch;
                VideoView.StretchDirection    = StretchDirection;

                // Wire up the layout updates
                VideoView.LayoutUpdated += HandleVideoViewLayoutUpdates;
            };

            // Set some default layout properties.
            UseLayoutRounding   = true;
            SnapsToDevicePixels = true;

            // Setup the content grid and add it as part of the user control
            Content = ContentGrid;

            // Set some layout defaults
            ContentGrid.HorizontalAlignment = HorizontalAlignment.Stretch;
            ContentGrid.VerticalAlignment   = VerticalAlignment.Stretch;
            ContentGrid.UseLayoutRounding   = true;
            ContentGrid.SnapsToDevicePixels = true;
            ContentGrid.IsHitTestVisible    = false;

            // Setup the Subtitle View
            SubtitlesView.FontSize            = 98;
            SubtitlesView.Padding             = new Thickness(0);
            SubtitlesView.FontFamily          = new FontFamily("Microsoft Sans Serif, Lucida Console, Calibri");
            SubtitlesView.FontStretch         = FontStretches.Condensed;
            SubtitlesView.FontWeight          = FontWeights.Bold;
            SubtitlesView.TextOutlineWidth    = new Thickness(4);
            SubtitlesView.TextForeground      = Brushes.LightYellow;
            SubtitlesView.UseLayoutRounding   = true;
            SubtitlesView.SnapsToDevicePixels = true;
            SubtitlesView.IsHitTestVisible    = false;
            SubtitlesView.Focusable           = false;
            SubtitlesView.HorizontalAlignment = HorizontalAlignment.Left;
            SubtitlesView.VerticalAlignment   = VerticalAlignment.Top;

            // Add the subtitles control and bind the attached properties
            Subtitles.SetForeground(this, SubtitlesView.TextForeground);
            BindProperty(this, Subtitles.ForegroundProperty, SubtitlesView, nameof(SubtitlesView.TextForeground), BindingMode.TwoWay);
            BindProperty(this, Subtitles.OutlineBrushProperty, SubtitlesView, nameof(SubtitlesView.TextOutline), BindingMode.TwoWay);
            BindProperty(this, Subtitles.OutlineWidthProperty, SubtitlesView, nameof(SubtitlesView.TextOutlineWidth), BindingMode.TwoWay);
            BindProperty(this, Subtitles.EffectProperty, SubtitlesView, nameof(SubtitlesView.TextForegroundEffect), BindingMode.TwoWay);
            BindProperty(this, Subtitles.FontSizeProperty, SubtitlesView, nameof(SubtitlesView.FontSize), BindingMode.TwoWay);
            BindProperty(this, Subtitles.FontWeightProperty, SubtitlesView, nameof(SubtitlesView.FontWeight), BindingMode.TwoWay);
            BindProperty(this, Subtitles.FontFamilyProperty, SubtitlesView, nameof(SubtitlesView.FontFamily), BindingMode.TwoWay);
            BindProperty(this, Subtitles.TextProperty, SubtitlesView, nameof(SubtitlesView.Text), BindingMode.TwoWay);

            // Position the Captions View
            CaptionsView.HorizontalAlignment = HorizontalAlignment.Left;
            CaptionsView.VerticalAlignment   = VerticalAlignment.Top;

            // Compose the control by adding overlapping children
            ContentGrid.Children.Add(VideoView);
            ContentGrid.Children.Add(SubtitlesView);
            ContentGrid.Children.Add(CaptionsView);

            // Display the control (or not)
            if (!Library.IsInDesignMode)
            {
                // Setup the media engine and property updates timer
                MediaCore = new MediaEngine(this, new MediaConnector(this));
            }
            else
            {
                var bitmap       = Properties.Resources.FFmpegMediaElementBackground;
                var bitmapSource = Imaging.CreateBitmapSourceFromHBitmap(
                    bitmap.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
                var controlBitmap = new WriteableBitmap(bitmapSource);
                VideoView.Source = controlBitmap;
            }

            // Bind Content View Properties
            BindProperty(VideoView, HorizontalAlignmentProperty, this, nameof(HorizontalContentAlignment), BindingMode.OneWay);
            BindProperty(VideoView, VerticalAlignmentProperty, this, nameof(VerticalContentAlignment), BindingMode.OneWay);
        }