XAML Defined StoryBoard throws nullpointer when called in C

I'm trying to animate a button so that the image zooms in when the user touches it and reverts to normal when he releases it, but somehow the XAML Defined StoryBoard throws a null pointer when called in C #. My code snippets are here, I don't know at all what is creating this problem.

XAML:

<UserControl x:Class="Mavie_Base.GUIElements.MenuButton"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
d:DesignHeight="200" d:DesignWidth="140">

<UserControl.Resources>
    <Storyboard x:Name="Enlarge">
        <DoubleAnimation Duration="0:0:1" From="60" To="66" Storyboard.TargetProperty="Height" Storyboard.TargetName="Rectangle"/>
    </Storyboard>
    <Storyboard x:Name="Normalize">
        <DoubleAnimation Duration="0:0:1" From="66" To="60" Storyboard.TargetProperty="Height" Storyboard.TargetName="Rectangle"/>
    </Storyboard>
</UserControl.Resources>
....
</UserControl>

      

FROM#:

private void IconContainer_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
    {
        Enlarge.Begin();
    }

    private void IconContainer_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
    {
        Normalize.Begin();
    }

      

+3


source to share


2 answers


Try to specify the target property:



Storyboard.TargetProperty="(FrameworkElement.Height)"

      

+3


source


+1 @thumbmunkeys

Although if it was me, it sounds like something you might want to add to other stuff later for interaction, so I would just add the functionality already built in for handling and then you have the option to add to it later if you want to add other functionality to your functionality. I would also think (not positively, should have checked) if you play around with the height property it might compensate for other artifacts laid out on the screen as it adjusts, creating a clumsy interface that won't be very user-friendly.

Then again, if it were me, I would probably just do something like this. First, instead of fiddling with the Height / Width properties that other parts of the screen might cause, click ScaleTransform.ScaleY

(and ScaleX respectively, you want to play with values ​​I'm sure to get the dimensions you want.) For your resizing.

I'll also probably go ahead and make it "light-hearted" and stripped down Button

for other options later and for handling my events like Pressed. Something like:

<Style x:Key="GrowOnPressedThingy" TargetType="Button">        
  <Setter Property="HorizontalContentAlignment" Value="Center" />
  <Setter Property="VerticalContentAlignment" Value="Center" />
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="Button">
        <Grid x:Name="Container"
              RenderTransformOrigin="0.5,0.5">
          <Grid.RenderTransform>
            <TransformGroup>
              <ScaleTransform />
              <SkewTransform />
              <RotateTransform />
              <TranslateTransform />
            </TransformGroup>
          </Grid.RenderTransform>
          <VisualStateManager.VisualStateGroups>
            <VisualStateGroup x:Name="CommonStates">
              <VisualStateGroup.Transitions>
                <!-- Just left for reference -->
              </VisualStateGroup.Transitions>
              <VisualState x:Name="Normal" />
              <VisualState x:Name="MouseOver"/>
              <VisualState x:Name="Pressed">
                <Storyboard>
                  <DoubleAnimationUsingKeyFrames Storyboard.TargetName="Container" 
                                                 Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)">
                    <EasingDoubleKeyFrame KeyTime="0:0:0.01" Value="1.05" />
                  </DoubleAnimationUsingKeyFrames>
                  <DoubleAnimationUsingKeyFrames Storyboard.TargetName="Container" 
                                                 Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)">
                    <EasingDoubleKeyFrame KeyTime="0:0:0.01" Value="1.05" />
                  </DoubleAnimationUsingKeyFrames>
                </Storyboard>
              </VisualState>
              <VisualState x:Name="Disabled"/>
            </VisualStateGroup>
            <VisualStateGroup x:Name="FocusStates">
              <VisualState x:Name="Focused"/>
              <VisualState x:Name="Unfocused" />
            </VisualStateGroup>
          </VisualStateManager.VisualStateGroups>

          <ContentControl x:Name="contentPresenter"
                          HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                          VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                          Content="{TemplateBinding Content}"
                          ContentTemplate="{TemplateBinding ContentTemplate}"
                                              IsTabStop="False"/>

        </Grid>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

      



So you can just quickly ...;

<Button Content="whatever/image/path/blah.png" 
        Style="{StaticResource GrowOnPressedThingy}"/>

      

... wherever you need it and you have it set up to easily add other neat UI tricks you might think of in the future. Just a shot in the dark, but it might be worth considering.

Anyway, hope this helps. Greetings

+4


source







All Articles