public void AddStateAtRuntime () { Rectangle rect = new Rectangle { Name = RootName }; VSMControl c = new VSMControl (); c.ApplyTemplate (); // Create a visual state in code which we will try to use to animate the template element called 'Grid' foreach (VisualStateGroup g in VisualStateManager.GetVisualStateGroups (c.TemplateGrid)) { VisualState s = new VisualState (); s.SetValue (FrameworkElement.NameProperty, "C"); s.Storyboard = CreateWidthStoryboard (RootName, 600, 700); g.States.Add (s); } // The template element "Grid" can't be found by the new storyboard CreateAsyncTest (c, () => { Assert.IsTrue (VisualStateManager.GoToState (c, "A", false), "#1"); Assert.Throws<InvalidOperationException> (() => VisualStateManager.GoToState (c, "C", false), "#2"); // Adding a new element called 'Grid' to the TestPanel does not work TestPanel.Children.Add (rect); Assert.Throws<InvalidOperationException> (() => VisualStateManager.GoToState (c, "C", false), "#3"); // The new element is not findable from the control Assert.IsNull (c.FindName (RootName), "#4"); Assert.AreSame (rect, TestPanel.FindName (RootName), "#5"); // Adding it to the template grid instead TestPanel.Children.Remove (rect); c.TemplateGrid.Children.Add (rect); // It's not findable from the panel, but it is from the Control Assert.AreSame (rect, c.FindName (RootName), "#6"); Assert.IsNull (TestPanel.FindName (RootName), "#7"); // Once it's findable from the control, the storyboard will resolve to the new element and succeed Assert.IsTrue (VisualStateManager.GoToState (c, "C", false), "#8"); }, () => { Assert.AreEqual (700, rect.Width, "#9"); // The template element 'Grid' is not changed Assert.IsTrue (Double.IsNaN (c.TemplateGrid.Width), "#10"); } ); }
/// <summary> /// Adds the notification states group. /// </summary> /// <param name="popupView">The popup view.</param> /// <param name="panel">The panel.</param> private void AddNotificationStatesGroup(PopupView popupView, Panel panel) { var uniqueId = Guid.NewGuid(); var groups = (IList<VisualStateGroup>)VisualStateManager.GetVisualStateGroups(_panel); var vsGroup = new VisualStateGroup(); groups.Add(vsGroup); vsGroup.SetValue(FrameworkElement.NameProperty, string.Format(CultureInfo.InvariantCulture, "NotificationState{0}", uniqueId)); var hideState = new VisualState(); var showState = new VisualState(); var showCenterState = new VisualState(); var showBusyState = new VisualState(); var showStoryboard = new Storyboard(); if (_position == PopupPosition.Top) PopupHelper.AddFlipFromTopAnimation(showStoryboard, popupView); else PopupHelper.AddSlideFromLeftAnimation(showStoryboard, popupView); showStoryboard.Completed += (o, args) => { if (_duration == 0) return; _timer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(_duration) }; _timer.Start(); _timer.Tick += (sender, eventArgs) => { if (!_isMouseOver) HideNotificationPopup(_timer); else _timeToClose = true; }; }; showState.Storyboard = showStoryboard; var hideStoryboard = new Storyboard(); switch (_position) { case PopupPosition.Right: hideStoryboard.Children.Add(PopupHelper.GetSlideToRightAnimation(popupView)); break; case PopupPosition.Top: PopupHelper.AddFlipToTopAnimation(hideStoryboard, popupView); break; default: hideStoryboard.Children.Add(PopupHelper.GetOpacityZeroAnimation(popupView)); break; } _removeControlsAction = () => { panel.Children.Remove(popupView); groups.Remove(vsGroup); if (_modalBorder != null) _parentPanel.Children.Remove(_modalBorder); }; hideStoryboard.Completed += (o, args) => RemoveControls(); hideState.Storyboard = hideStoryboard; var showCenterStoryboard = new Storyboard(); showCenterState.Storyboard = showCenterStoryboard; var showBusyStoryboard = new Storyboard(); showBusyState.Storyboard = showBusyStoryboard; _showName = string.Format(CultureInfo.InvariantCulture, "Show{0}", uniqueId); _hideName = string.Format(CultureInfo.InvariantCulture, "Hide{0}", uniqueId); _showCenterName = string.Format(CultureInfo.InvariantCulture, "ShowCenter{0}", uniqueId); _showBusyName = string.Format(CultureInfo.InvariantCulture, "ShowBusy{0}", uniqueId); showState.SetValue(FrameworkElement.NameProperty, _showName); hideState.SetValue(FrameworkElement.NameProperty, _hideName); showCenterState.SetValue(FrameworkElement.NameProperty, _showCenterName); showBusyState.SetValue(FrameworkElement.NameProperty, _showBusyName); vsGroup.States.Add(showState); vsGroup.States.Add(showCenterState); vsGroup.States.Add(hideState); vsGroup.States.Add(showBusyState); }
public void ManagedVisualStates () { Rectangle r = new Rectangle { Name = "rect", Width = 0 }; VisualStateGroup group = new VisualStateGroup (); group.SetValue (FrameworkElement.NameProperty, "group"); VisualState vstate = new VisualState (); vstate.SetValue (FrameworkElement.NameProperty, "state"); DoubleAnimation anim = new DoubleAnimation { From = 100, To = 200, Duration = TimeSpan.FromSeconds (1) }; anim.SetValue (Control.NameProperty, "ANIMATION"); Storyboard.SetTargetName (anim, "rect"); Storyboard.SetTargetProperty (anim, new PropertyPath ("Width")); Storyboard sb = new Storyboard (); sb.Children.Add (anim); vstate.Storyboard = sb; group.States.Add (vstate); Enqueue (() => TestPanel.Children.Add (r)); Enqueue (() => VisualStateManager.GetVisualStateGroups ((FrameworkElement)TestPanel.Parent).Add (group)); Enqueue (() => Assert.AreEqual(0, r.Width, "#1")); Enqueue (() => Assert.IsTrue (VisualStateManager.GoToState ((Control) TestPage, "state", false), "#2")); Enqueue (() => Assert.IsGreater (99, r.Width, "#3")); Enqueue (() => TestPanel.Children.Clear ()); Enqueue (() => VisualStateManager.GetVisualStateGroups (TestPage).Clear ()); EnqueueTestComplete(); }
public void ManagedVisualStates2 () { Rectangle r = new Rectangle { Width = 0 }; VisualStateGroup group = new VisualStateGroup (); group.SetValue (FrameworkElement.NameProperty, "group2"); // Create the first visual state VisualState vstate = new VisualState (); vstate.SetValue (FrameworkElement.NameProperty, "state2"); DoubleAnimation anim = new DoubleAnimation { From = 100, To = 200, Duration = TimeSpan.FromSeconds (1) }; Storyboard.SetTarget (anim, r); Storyboard.SetTargetProperty (anim, new PropertyPath ("Width")); Storyboard sb = new Storyboard (); sb.Children.Add (anim); vstate.Storyboard = sb; group.States.Add (vstate); // Create the second visual state VisualState vstate2 = new VisualState (); vstate2.SetValue (FrameworkElement.NameProperty, "state3"); Storyboard sb2 = new Storyboard (); Storyboard.SetTarget (sb2, r); Storyboard.SetTargetProperty (sb2, new PropertyPath ("Width")); vstate2.Storyboard = sb2; group.States.Add (vstate2); // Ensure that the values reset correct Enqueue (() => TestPanel.Children.Add (r)); Enqueue (() => VisualStateManager.GetVisualStateGroups ((FrameworkElement)TestPanel.Parent).Add (group)); Enqueue (() => Assert.AreEqual(0, r.Width, "#1")); Enqueue (() => Assert.IsTrue (VisualStateManager.GoToState (TestPage, "state2", false), "#2")); Enqueue (() => Assert.IsGreater (99, r.Width, "#3")); Enqueue (() => Assert.IsTrue (VisualStateManager.GoToState (TestPage, "state3", false), "#3")); Enqueue (() => Assert.AreEqual (0, r.Width, "#4")); Enqueue (() => Assert.AreEqual (ClockState.Filling, sb2.GetCurrentState (), "#5")); Enqueue (() => TestPanel.Children.Clear ()); Enqueue (() => VisualStateManager.GetVisualStateGroups (TestPage).Clear ()); EnqueueTestComplete(); }