Esempio n. 1
0
        static void StartAnimation(DependencyObject target, CSSEquivalent cssEquivalent, double?from, object to, Duration Duration, EasingFunctionBase easingFunction, string visualStateGroupName, Action callbackForWhenfinished = null)
        {
            if (cssEquivalent.Name != null && cssEquivalent.Name.Count != 0)
            {
                UIElement uiElement = cssEquivalent.UIElement ?? (target as UIElement); // If no UIElement is specified, we assume that the property is intended to be applied to the instance on which the PropertyChanged has occurred.

                bool hasTemplate = (uiElement is Control) && ((Control)uiElement).HasTemplate;

                if (!hasTemplate || cssEquivalent.ApplyAlsoWhenThereIsAControlTemplate)
                {
                    if (cssEquivalent.DomElement == null && uiElement != null)
                    {
                        cssEquivalent.DomElement = uiElement.INTERNAL_OuterDomElement; // Default value
                    }
                    if (cssEquivalent.DomElement != null)
                    {
                        if (cssEquivalent.Value == null)
                        {
                            cssEquivalent.Value = (finalInstance, value) => { return(value ?? ""); }; // Default value
                        }
                        object cssValue = cssEquivalent.Value(target, to);



                        object newObj = CSHTML5.Interop.ExecuteJavaScriptAsync(@"new Object()");

                        if (from == null)
                        {
                            foreach (string csspropertyName in cssEquivalent.Name)
                            {
                                CSHTML5.Interop.ExecuteJavaScriptAsync(@"
$0[$1] = $2;", newObj, csspropertyName, cssValue);
                            }
                        }
                        else
                        {
                            foreach (string csspropertyName in cssEquivalent.Name)
                            {
                                CSHTML5.Interop.ExecuteJavaScriptAsync(@"
$0[$1] = [$2, $3];", newObj, csspropertyName, cssValue, from);
                            }
                        }

                        AnimationHelpers.CallVelocity(cssEquivalent.DomElement, Duration, easingFunction, visualStateGroupName, callbackForWhenfinished, newObj);
                    }
                }
            }
            else
            {
                throw new InvalidOperationException("Please set the Name property of the CSSEquivalent class.");
            }
        }
Esempio n. 2
0
        static bool TryStartAnimation(DependencyObject target, CSSEquivalent cssEquivalent, Color?from, object to, Duration Duration, EasingFunctionBase easingFunction, string visualStateGroupName, Action callbackForWhenfinished = null)
        {
            if (cssEquivalent.Name != null && cssEquivalent.Name.Count != 0)
            {
                UIElement uiElement = cssEquivalent.UIElement ?? (target as UIElement); // If no UIElement is specified, we assume that the property is intended to be applied to the instance on which the PropertyChanged has occurred.

                bool hasTemplate = (uiElement is Control) && ((Control)uiElement).HasTemplate;

                if (!hasTemplate || cssEquivalent.ApplyAlsoWhenThereIsAControlTemplate)
                {
                    if (cssEquivalent.DomElement == null && uiElement != null)
                    {
                        cssEquivalent.DomElement = uiElement.INTERNAL_OuterDomElement; // Default value
                    }
                    if (cssEquivalent.DomElement != null)
                    {
                        if (cssEquivalent.Value == null)
                        {
                            cssEquivalent.Value = (finalInstance, value) =>
                            {
                                if (value == null)
                                {
                                    return("");
                                }
                                else
                                {
                                    Dictionary <string, object> valuesDict = new Dictionary <string, object>();
                                    foreach (string name in cssEquivalent.Name)
                                    {
                                        if (!name.EndsWith("Alpha"))
                                        {
                                            valuesDict.Add(name, ((Color)value).INTERNAL_ToHtmlStringForVelocity());
                                        }
                                        else
                                        {
                                            valuesDict.Add(name, ((double)((Color)value).A) / 255);
                                        }
                                    }
                                    return(valuesDict);
                                }
                                //return value ?? "";
                            };     // Default value
                        }
                        object cssValue = cssEquivalent.Value(target, to);

                        //INTERNAL_HtmlDomManager.SetDomElementStyleProperty(cssEquivalent.DomElement, cssEquivalent.Name, cssValue);


                        object newObj = CSHTML5.Interop.ExecuteJavaScriptAsync(@"new Object()");

                        if (AnimationHelpers.IsValueNull(from)) //todo: when using Bridge, I guess we would want to directly use "from == null" since it worked in the first place (I think).
                        {
                            if (!(cssValue is Dictionary <string, object>))
                            {
                                foreach (string csspropertyName in cssEquivalent.Name)
                                {
                                    //todo: check the note below once the clone will work properly (a value set through velocity is not set in c#, which makes the clone take the former value).
                                    //Note: the test below is to avoid setting Background because Velocity cannot handle it,
                                    //      which makes the element go transparent (no color) before then changing color with backgroundColor.
                                    //      Therefore, we no longer go in the animation from the previous color to the new one but from no color to the new one
                                    if (csspropertyName != "background") //todo: when we will be able to use velocity for linearGradientBrush, we will need another solution here.
                                    {
                                        CSHTML5.Interop.ExecuteJavaScriptAsync(@"$0[$1] = $2;", newObj, csspropertyName, cssValue);
                                    }
                                }
                            }
                            else
                            {
                                Dictionary <string, object> cssValueAsDictionary = (Dictionary <string, object>)cssValue;
                                foreach (string csspropertyName in cssEquivalent.Name)
                                {
                                    //todo: check the note below once the clone will work properly (a value set through velocity is not set in c#, which makes the clone take the former value).
                                    //Note: the test below is to avoid setting Background because Velocity cannot handle it,
                                    //      which makes the element go transparent (no color) before then changing color with backgroundColor.
                                    //      Therefore, we no longer go in the animation from the previous color to the new one but from no color to the new one
                                    if (csspropertyName != "background") //todo: when we will be able to use velocity for linearGradientBrush, we will need another solution here.
                                    {
                                        CSHTML5.Interop.ExecuteJavaScriptAsync(@"$0[$1] = $2;", newObj, csspropertyName, cssValueAsDictionary[csspropertyName]);
                                    }
                                }
                            }
                        }
                        else
                        {
                            object fromCssValue = cssEquivalent.Value(target, from);
                            foreach (string csspropertyName in cssEquivalent.Name)
                            {
                                //todo: check the note below once the clone will work properly (a value set through velocity is not set in c#, which makes the clone take the former value).
                                //Note: the test below is to avoid setting Background because Velocity cannot handle it,
                                //      which makes the element go transparent (no color) before then changing color with backgroundColor.
                                //      Therefore, we no longer go in the animation from the previous color to the new one but from no color to the new one
                                if (csspropertyName != "background") //todo: when we will be able to use velocity for linearGradientBrush, we will need another solution here.
                                {
                                    object currentCssValue = cssValue;
                                    if (cssValue is Dictionary <string, object> )
                                    {
                                        currentCssValue = ((Dictionary <string, object>)cssValue)[csspropertyName];
                                    }
                                    object currentFromCssValue = fromCssValue;
                                    if (fromCssValue is Dictionary <string, object> )
                                    {
                                        currentFromCssValue = ((Dictionary <string, object>)fromCssValue)[csspropertyName];
                                    }
                                    CSHTML5.Interop.ExecuteJavaScriptAsync(@"$0[$1] = [$2, $3];", newObj, csspropertyName, currentCssValue, currentFromCssValue);
                                }
                            }
                        }

                        AnimationHelpers.CallVelocity(cssEquivalent.DomElement, Duration, easingFunction, visualStateGroupName, callbackForWhenfinished, newObj);

                        return(true);
                    }
                }
                return(false);
            }
            else
            {
                throw new InvalidOperationException("Please set the Name property of the CSSEquivalent class.");
            }
        }