无法解析TargetName - Silverlight4 C#

Jac*_*lor 8 c# silverlight animation silverlight-4.0

我收到错误无法解析TargetName grdGeneral.我要做的是有一个淡出功能,它接受一个网格并将不透明度淡化为零.我有这个功能要求的MouseLeftButtonDown和加载的XAML和形式加载.

呼唤淡出:

private void imgNext_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            fadeOut(grdGeneral);            
        }
Run Code Online (Sandbox Code Playgroud)

淡出功能:

private void fadeOut(Grid pGrid)
        {
            Storyboard stb = new Storyboard();

            DoubleAnimation da = new DoubleAnimation();
            da.From = 1.0;
            da.To = 0.0;

            stb.Duration = new Duration(TimeSpan.FromSeconds(.75));
            stb.Children.Add(da);

            Storyboard.SetTargetName(da, pGrid.Name);
            Storyboard.SetTargetProperty(da, new PropertyPath(Grid.OpacityProperty));

            stb.Begin();
        }
Run Code Online (Sandbox Code Playgroud)

我一直在一些教程网站上,我的代码似乎遵循相同的顺序.在你说重新发布之前我也一直在讨论这个stackoverflow 问题.这个问题必须处理多个问题,我只是想开始动画.

堆栈跟踪

System.InvalidOperationException was unhandled by user code
  Message=Cannot resolve TargetName grdGeneral.
  StackTrace:
       at MS.Internal.XcpImports.MethodEx(IntPtr ptr, String name, CValue[] cvData)
       at MS.Internal.XcpImports.MethodEx(DependencyObject obj, String name)
       at System.Windows.Media.Animation.Storyboard.Begin()
       at MeterTesting.QuarterReportGUI.fadeOut(Grid pGrid)
       at MeterTesting.QuarterReportGUI.imgNext_MouseLeftButtonDown(Object sender, MouseButtonEventArgs e)
       at MS.Internal.CoreInvokeHandler.InvokeEventHandler(Int32 typeIndex, Delegate handlerDelegate, Object sender, Object args)
       at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, String eventName)
  InnerException: 
Run Code Online (Sandbox Code Playgroud)

小智 5

而不是使用

 Storyboard.SetTargetName(da, pGrid.Name);
Run Code Online (Sandbox Code Playgroud)

尝试

 Storyboard.SetTarget(da, pGrid);
Run Code Online (Sandbox Code Playgroud)


Phi*_*hil 3

如果不是绝对必要的话,您真的不应该尝试像这样编写故事板。一旦你的动画变得有点复杂,它就会困扰你。

您应该在 xaml 中执行此操作,最好使用 Blend。在你的 xaml 中试试这个:

<UserControl.Resources>
    <Storyboard x:Name="FadeGrid">
        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="grdGeneral">
            <EasingDoubleKeyFrame KeyTime="0" Value="1"/>
            <EasingDoubleKeyFrame KeyTime="0:0:0.7" Value="0"/>
        </DoubleAnimationUsingKeyFrames>
    </Storyboard>
</UserControl.Resources>

<Grid x:Name="grdGeneral" Background="White">
    <Image x:Name="imgNext" MouseLeftButtonDown="imgNext_MouseLeftButtonDown" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Source="/StoryboardInCode;component/Images/avatar.jpg"></Image>
</Grid>
Run Code Online (Sandbox Code Playgroud)

在后面的代码中,您可以像这样调用它:

private void imgNext_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        FadeGrid.Begin();
    }
Run Code Online (Sandbox Code Playgroud)

这应该会给你你正在寻找的东西。

使用按钮而不是图像也会更好。