C# XAML定义的情节提要在C中调用时抛出空指针

C# XAML定义的情节提要在C中调用时抛出空指针,c#,silverlight,xaml,windows-phone-8,C#,Silverlight,Xaml,Windows Phone 8,我正在尝试设置一个按钮的动画,这样当用户触摸按钮时图像会放大,当用户释放按钮时图像会恢复正常,但在C#中调用XAML定义的故事板时,不知何故会抛出一个空指针。我的代码片段在下面,我根本不知道是什么造成了这个问题 XAML: 尝试强制转换targetproperty: Storyboard.TargetProperty="(FrameworkElement.Height)" +1@thumbmunkeys 如果是我的话,听起来你可能想在以后的交互中添加其他东西,所以我只想让已经内置的功能来处理这

我正在尝试设置一个按钮的动画,这样当用户触摸按钮时图像会放大,当用户释放按钮时图像会恢复正常,但在C#中调用XAML定义的故事板时,不知何故会抛出一个空指针。我的代码片段在下面,我根本不知道是什么造成了这个问题

XAML:


尝试强制转换targetproperty:

Storyboard.TargetProperty="(FrameworkElement.Height)"
+1@thumbmunkeys

如果是我的话,听起来你可能想在以后的交互中添加其他东西,所以我只想让已经内置的功能来处理这个问题,然后如果你想在功能中添加其他东西,就有空间在以后添加。我还认为(不是肯定的,必须检查)如果你去修补height属性,它可能会抵消屏幕上显示的其他工件,因为它会调整,导致用户界面不太舒服

再说一次,如果是我,我可能会做类似的事情。首先,不要摆弄可能会影响屏幕其他部分的高度/宽度属性,而是点击
ScaleTransform.ScaleY
(和ScaleX分别,您需要使用我确定的值来获得所需的大小)以更改大小

我也可能会继续,把它变成一个“无外观”的按钮,并去掉
按钮
,以后再做其他选择,处理我的事件,比如按下按钮。有点像

<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>

这样你就可以做一个快速的

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

…无论您在哪里需要它,并且您已经将其设置为可以轻松添加您将来可能想到的其他整洁的UI技巧。只是在黑暗中拍摄,但也许值得考虑


不管怎样,希望这有帮助。干杯

+1这肯定会更有效,不过如果我没有弄错的话,更改宽度/高度会影响布局,而修改“渲染变换”则不会t@thumbmunkeys是的,但是你的回答了OP的初衷,这是一个陷阱。我已经删除了这个问题,有点过于热情,意识到我已经做了类似的事情。但是谢谢你的快速回复。啊,酷,我想我也会删除我的。不过现在。。。。午饭时间。如果您有任何问题,请告诉我们。干杯:)我实际上没有在这里使用按钮,而是在网格中使用一个矩形,带有一个不透明的掩码(图标)。我目前无法看到如何将代码转换为适用于几乎任何图形元素的方式,如果能做到这一点,这将是一段非常有用的代码,不仅对我,而且对几乎每个windows phone开发人员都是如此。你的答案肯定是正确的,但既然Chris也发布了答案,我也要检查他的答案,看看哪一个更符合我的情况,这样我就可以接受最好的答案,但我的+1是正确的。
<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>
<Button Content="whatever/image/path/blah.png" 
        Style="{StaticResource GrowOnPressedThingy}"/>