77332

Using Dependency object for color animation WP8

Question:

I'm using a button where I've shaped it, in its ControlTemplate. I've also added a storyboard in the controlTemplate. The storyboard changes the boarder of the element in my controlTemplate. I access this from the code behind and activate it, the problem is when I do this on the phone there is a lag. I've structured my code after the MVVM structure, my view is:

<Button x:Name="Button1" BorderThickness="0" BorderBrush="Transparent"> <Button.Template> <ControlTemplate x:Name="Control"> <Path x:Name="Control2" Style="{StaticResource style_ColorButton}" Data="{Binding Data}" Fill="{StaticResource Background}"> <Path.Resources> <Storyboard x:Name="StoryBoard1"> <ColorAnimation Storyboard.TargetName="Control2" Storyboard.TargetProperty="(Stroke).(SolidColorBrush.Color)" To="Blue" Duration="0"/> </Storyboard> </Path.Resources> </Path> </ControlTemplate> </Button.Template>

And my the part of the ViewModel where I activate my storyboard:

foreach (UIElement x in ElementsAtPoint) { f = x as FrameworkElement; if (f is Path) { try { h = f as Path; Storyboard sb = h.Resources["StoryBoard1"] as Storyboard; sb.Begin(); } catch { } break; } }

I've <a href="https://stackoverflow.com/questions/22283799/viewport-origin-animation/22478865?noredirect=1#comment34229911_22478865" rel="nofollow">read</a> that one can use Dependency object for the animation, but I'm not sure how it works or if it will work, but any help trying to implement dependency object for the animation would be appreciated.

Answer1:

I would recommend using VisualStates to do what you are looking for. I modified the Style of a button to add a story to the MouseOver VisualState, then added event listeners for MouseEnter and MouseLeave to the button. These events are fired when you touch the device, and drag your finger over an element, and then drag it off again. You could modify the code below to check if something is being dragged. You could also take a look at <a href="http://www.bing.com/search?q=windows%20phone%20drag%20drop&go=Submit&qs=n&form=QBRE&pq=windows%20phone%20drag%20drop&sc=2-23&sp=-1&sk=&cvid=1104155eec8d40e496a7fd613aac4396" rel="nofollow">Drag/Drop functionality</a>.

Using the following Style

<Style x:Key="ButtonStyle" TargetType="Button"> <Setter Property="Background" Value="Transparent"/> <Setter Property="BorderBrush" Value="{StaticResource PhoneForegroundBrush}"/> <Setter Property="Foreground" Value="{StaticResource PhoneForegroundBrush}"/> <Setter Property="BorderThickness" Value="{StaticResource PhoneBorderThickness}"/> <Setter Property="FontFamily" Value="{StaticResource PhoneFontFamilySemiBold}"/> <Setter Property="FontSize" Value="{StaticResource PhoneFontSizeMedium}"/> <Setter Property="Padding" Value="10,5,10,6"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="Button"> <Grid Background="Transparent"> <VisualStateManager.VisualStateGroups> <VisualStateGroup x:Name="CommonStates"> <VisualState x:Name="Normal"/> <VisualState x:Name="MouseOver"> <Storyboard> <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentContainer"> <DiscreteObjectKeyFrame KeyTime="0" Value="White"/> </ObjectAnimationUsingKeyFrames> <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="ButtonBackground"> <DiscreteObjectKeyFrame KeyTime="0" Value="Orange"/> </ObjectAnimationUsingKeyFrames> </Storyboard> </VisualState> <VisualState x:Name="Pressed"> <Storyboard> <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentContainer"> <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneButtonBasePressedForegroundBrush}"/> </ObjectAnimationUsingKeyFrames> <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="ButtonBackground"> <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneAccentBrush}"/> </ObjectAnimationUsingKeyFrames> </Storyboard> </VisualState> <VisualState x:Name="Disabled"> <Storyboard> <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentContainer"> <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneDisabledBrush}"/> </ObjectAnimationUsingKeyFrames> <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="ButtonBackground"> <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneDisabledBrush}"/> </ObjectAnimationUsingKeyFrames> <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="ButtonBackground"> <DiscreteObjectKeyFrame KeyTime="0" Value="Transparent"/> </ObjectAnimationUsingKeyFrames> </Storyboard> </VisualState> </VisualStateGroup> </VisualStateManager.VisualStateGroups> <Border x:Name="ButtonBackground" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" CornerRadius="0" Margin="{StaticResource PhoneTouchTargetOverhang}"> <ContentControl x:Name="ContentContainer" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" Foreground="{TemplateBinding Foreground}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" Padding="{TemplateBinding Padding}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"/> </Border> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style>

Notice it has a MouseOver VisualState. Then assign the style and subscribe to the event handlers

<Button Content="Drag over me" Style="{StaticResource ButtonStyle}" MouseEnter="OnButtonMouseEnter" MouseLeave="OnButtonMouseLeave"/>

And in the event handler change the visual state.

private void OnButtonMouseEnter(object sender, MouseEventArgs e) { VisualStateManager.GoToState((Control)sender, "MouseOver", true); } private void OnButtonMouseLeave(object sender, MouseEventArgs e) { VisualStateManager.GoToState((Control)sender, "Normal", true); }

With this, when you tap and drag your finger over the button, it will turn orange with white text.

Recommend

  • Specified element is already the logical child of another element. Disconnect it first”
  • how do I collapse parent content if all children are collapsed
  • Get the UI object from a data object
  • Save the generated pdf directly to the server directory folder without user prompt
  • All GIT patches I create throw fatal: unrecognized input
  • Can find symbol variable Base64 what is the reason? [closed]
  • xp_regread() returned error 5, 'Access is denied.'
  • Why do I get an Invalid Operation Exception (non STA thread?) running this MSpec test on TeamCity?
  • Faster implementation for reduceByKey on Seq of pairs possible?
  • Azure ServiceBus Token Expired
  • Loop through all buttons on screen for a given tag, WP7, using C#
  • Creating a database programatically in nlog to enable using DatabaseTarget
  • Virtualizing WrapPanel as ListView's ItemsTemplate
  • My c# ComboBox does not call the getter after selecting an item. Why?
  • EclEmma, Java8 and Lambda - no coverage on lambda expression
  • WPF RichTextBox scroll to TextPointer
  • How to change EasingDoubleKeyFrame value at Runtime?
  • AngularJS ui-router - template not displaying
  • _Layout.cshtml cannot be requested directly because it calls the “RenderBody” method
  • Cannot convert value of type 'X' to expected argument type 'X'
  • java use Regular Expressions to generate a string [duplicate]
  • Why is JSON.NET adding all these backslashes
  • Prevent page break in text block with iText, XMLWorker
  • Enumerating Controls on a Form
  • Shouldn't else be indented in the below code
  • Extract All Possible Paths from Expression-Tree and evaluate them to hold TRUE
  • XSLT foreach repeating nodes to flat
  • Creating a DropDownList
  • List images(01.png) and descriptions(01.txt) from directory
  • WPF - CanExecute dosn't fire when raising Commands from a UserControl
  • D3 nodes and links from JSON with nested arrays of children
  • Reading JSON from a file using C++ REST SDK (Casablanca)
  • Why value captured by reference in lambda is broken? [duplicate]
  • Pass value from viewmodel to script in zk
  • Nant, Vault & Windows Integrated Authentication
  • What is Eclipse's Declaration View used for?
  • javascript inside java/jsp code
  • Jquery - Jquery Wysiwyg return html as a string
  • SVN: Merging two branches together
  • Append folder name and increment by 1 using batch script