微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

无法解析TargetName – Silverlight4 C#

我收到错误无法解析TargetName GrdGeneral.我要做的是有一个淡出功能,它接受一个网格并将不透明度淡化为零.我在MouseLeftButtonDown上调用了这个函数,并在加载了xaml和form之后加载.

呼唤淡出:

private void imgNext_MouseLeftButtonDown(object sender,MouseButtonEventArgs e)
        {
            fadeOut(GrdGeneral);            
        }

淡出功能

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();
        }

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

堆栈跟踪

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:

解决方法

如果你不是必须的话,你真的不应该尝试编写这样的故事板.一旦你的动画变得有点复杂,它就会咬你.

您应该在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>

在您的代码中,您将调用它,如下所示:

private void imgNext_MouseLeftButtonDown(object sender,MouseButtonEventArgs e)
    {
        FadeGrid.Begin();
    }

这应该会给你你想要的东西.

使用按钮代替图像也会更好.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐