예제 #1
0
        public static void DetachVisualChildIfNotNull(UIElement child, UIElement parent)
        {
#if PERFSTAT
            var t = Performance.now();
#endif
            if (child != null && IsElementInVisualTree(child)) //todo: doublecheck that "IsElementInVisualTree" is a good thing here.
            {
                // Verify that the child is really a child of the specified control:
                if (parent.INTERNAL_VisualChildrenInformation != null &&
                    parent.INTERNAL_VisualChildrenInformation.ContainsKey(child))
                {
                    // Remove the element from the DOM:
                    string stringForDebugging = !IsRunningInJavaScript() ? "Removing " + child.GetType().ToString() : null;
                    INTERNAL_HtmlDomManager.RemoveFromDom(child.INTERNAL_AdditionalOutsideDivForMargins, stringForDebugging);

                    // Remove the parent-specific wrapper around the child in the DOM (if any):
                    var optionalChildWrapper_OuterDomElement = parent.INTERNAL_VisualChildrenInformation[child].INTERNAL_OptionalChildWrapper_OuterDomElement;
                    if (optionalChildWrapper_OuterDomElement != null)
                    {
                        INTERNAL_HtmlDomManager.RemoveFromDom(optionalChildWrapper_OuterDomElement);
                    }

                    // Remove the element from the parent's children collection:
                    parent.INTERNAL_VisualChildrenInformation.Remove(child);

                    child.INTERNAL_SpanParentCell = null;

                    // Detach the element as well as all the children recursively:
                    DetachVisualChidrenRecursively(child);


                    INTERNAL_WorkaroundIE11IssuesWithScrollViewerInsideGrid.RefreshLayoutIfIE();

                    child.InvalidateMeasure();
                    child.InvalidateArrange();
                    parent.InvalidateArrange();
                }
                else
                {
                    throw new Exception(
                              string.Format("Cannot detach the element '{0}' because it is not a child of the element '{1}'.",
                                            child.GetType().ToString(),
                                            parent.GetType().ToString()));
                }
            }
#if PERFSTAT
            Performance.Counter("DetachVisualChildIfNotNull", t);
#endif
        }
예제 #2
0
        //private double GetColumnActualHeight_CSSVersion(ColumnDefinition columnDefinition)
        //{
        //    //the column's height is basically the height of the whole grid, right?
        //    if (CSharpXamlForHtml5.Environment.IsRunningInJavaScript)
        //    {
        //        return _innerDiv.offsetHeight;
        //    }
        //    else
        //    {
        //        INTERNAL_SimulatorExecuteJavaScript.ForceExecutionOfAllPendingCode(); // Explanation: we usually optimize performance in the Simulator by postponing the JS code that sets the CSS properties. This reduces the number of interop calls between C# and the browser. However, in the current case here we need to have all the properties already applied in order to be able to calculate the size of the DOM element. Therefore we need to call the "ForceExecution" method.
        //        return (double)INTERNAL_HtmlDomManager.CastToJsValue_SimulatorOnly(INTERNAL_HtmlDomManager.GetDomElementAttribute(_innerDiv, "offsetHeight"));
        //    }
        //}

        private double GetRowActualHeight_CSSVersion(RowDefinition rowDefinition)
        {
            double returnValue = double.NaN;

            //make a new div that we add to the grid in the correct column, with width and height at 100%, (opacity at 0 ?), position: absolute
            int rowIndex = _rowDefinitionsOrNull.IndexOf(rowDefinition);

            dynamic div1 = AddTemporaryDivForRowOrColumnDimensions(0, rowIndex);

            if (CSharpXamlForHtml5.Environment.IsRunningInJavaScript)
            {
                returnValue = div1.offsetHeight;
            }
            else
            {
                returnValue = Convert.ToDouble(INTERNAL_HtmlDomManager.GetDomElementAttribute(div1, "offsetHeight"));
            }

            INTERNAL_HtmlDomManager.RemoveFromDom(div1);

            return(returnValue);
        }
예제 #3
0
        private double GetColumnActualWidth_CSSVersion(ColumnDefinition columnDefinition)
        {
            double returnValue = 0;

            //make a new div that we add to the grid in the correct column, with width and height at 100%, (opacity at 0 ?), position: absolute
            int columnIndex = _columnDefinitionsOrNull.IndexOf(columnDefinition);

            var div1 = AddTemporaryDivForRowOrColumnDimensions(columnIndex, 0);

            if (CSharpXamlForHtml5.Environment.IsRunningInJavaScript)
            {
                returnValue = ((dynamic)div1).offsetWidth;
            }
            else
            {
                returnValue = Convert.ToDouble(INTERNAL_HtmlDomManager.GetDomElementAttribute(div1, "offsetWidth"));
            }

            INTERNAL_HtmlDomManager.RemoveFromDom(div1);

            return(returnValue);
        }
예제 #4
0
        private static void Source_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var control  = (MediaElement)d;
            var newValue = (Uri)e.NewValue;

            // Always check that the control is in the Visual Tree before modifying its HTML representation
            if (INTERNAL_VisualTreeManager.IsElementInVisualTree(control))
            {
                string newUri = newValue.ToString();

                // If the new Source is an empty string, we avoid the error messages:
                if (!string.IsNullOrWhiteSpace(newUri))
                {
                    string tagString = "none";

                    string valueForHtml5SourceProperty = INTERNAL_UriHelper.ConvertToHtml5Path(newValue.ToString(), control);

                    string newExtensionLowercase = GetExtension(newUri).ToLower();

                    if (SupportedVideoTypes.Contains(newExtensionLowercase))
                    {
                        if (control.IsAudioOnly || control._mediaElement == null) //note: I chose to use IsAudioOnly here because using e.oldValue would make it recreate the video tag when it was already a video tag.
                        {
                            tagString           = "video";
                            control.IsAudioOnly = false;
                        }
                    }
                    else if (SupportedAudioTypes.Contains(newExtensionLowercase))
                    {
                        if (!control.IsAudioOnly || control._mediaElement == null) //note: I chose to use IsAudioOnly here because using e.oldValue would make it recreate the audio tag when it was already a audio tag.
                        {
                            tagString           = "audio";
                            control.IsAudioOnly = true;
                        }
                    }
                    else
                    {
                        throw new NotSupportedException("ERROR: The MediaElement control only supports files of the following types: VIDEO: mp4, ogv, webm, 3gp - AUDIO: mp3, ogg - Note: for best browser compatibility, it is recommended to use only MP3 and MP4 files.");
                    }
                    if (tagString != "none") //tagString != "none" means that the new Uri has a different type (audio VS video) than the old one, so we need to (re)create the dom tag.
                    {
                        if (control._mediaElement != null)
                        {
                            INTERNAL_HtmlDomManager.RemoveFromDom(control._mediaElement); //note: there can be only one child element.
                        }
#if OPENSILVER
                        if (!INTERNAL_InteropImplementation.IsRunningInTheSimulator_WorkAround() || tagString == "video")
#else
                        if (CSharpXamlForHtml5.Environment.IsRunningInJavaScript || tagString == "video")
#endif
                        {
                            object element      = null;
                            object outerDiv     = control.INTERNAL_OuterDomElement;
                            var    elementStyle = INTERNAL_HtmlDomManager.CreateDomElementAppendItAndGetStyle(tagString, outerDiv, control, out element);

                            control._mediaElement_ForAudioOnly_ForSimulatorOnly = null;

                            control._mediaElement = element;

                            if (tagString == "video")
                            {
                                elementStyle.width  = "100%";
                                elementStyle.height = "100%";
                            }

                            control.Refresh(); //we refresh all the values of the element in the visual tree
                        }
                        else //when we are in the simulator, we don't want to use the <audio> tag, we will use a wpf one instead (because awesomium doesn't support .mp3 for example)
                        {
#if !CSHTML5NETSTANDARD
                            if (control._mediaElement_ForAudioOnly_ForSimulatorOnly == null)
                            {
                                control._mediaElement_ForAudioOnly_ForSimulatorOnly = INTERNAL_Simulator.WpfMediaElementFactory.Create((Action)control.SimulatorMediaElement_Loaded, (Action)control.SimulatorMediaElement_MediaEnded);
                            }
                            control._mediaElement_ForAudioOnly_ForSimulatorOnly.Source = new Uri(valueForHtml5SourceProperty);
                            control.Refresh_SimulatorOnly();
#endif
                            return;
                        }
                    }

                    // Update the "src" property of the <video> or <audio> tag
                    INTERNAL_HtmlDomManager.SetDomElementAttribute(control._mediaElement, "src", valueForHtml5SourceProperty, forceSimulatorExecuteImmediately: true);
                }
                else
                {
                    if (control._mediaElement != null)
                    {
                        // Remove previous video/audio if any:
                        INTERNAL_HtmlDomManager.SetDomElementAttribute(control._mediaElement, "src", "", forceSimulatorExecuteImmediately: true);
                    }
                }
            }
        }