【问题标题】:Cannot resolve TargetName - Silverlight4 C#无法解析 TargetName - Silverlight4 C#
【发布时间】:2010-03-24 20:17:52
【问题描述】:

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

调用淡出:

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: 

【问题讨论】:

  • 可能是 Silverlight4 中的一个错误。对于那些有兴趣使用 Phil 的方式并且比尝试在代码中找到方式更好的人。 betaforums.silverlight.net/forums/p/23517/84087.aspx
  • 遇到了同样的问题。我绝对需要运行时创建的东西。解决方案是什么?上面的链接坏了。

标签: c# silverlight animation silverlight-4.0


【解决方案1】:

而不是使用

 Storyboard.SetTargetName(da, pGrid.Name);

试试

 Storyboard.SetTarget(da, pGrid);

【讨论】:

    【解决方案2】:

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

    您应该改为在 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();
        }
    

    这应该可以满足您的需求。

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

    【讨论】:

    • 感谢代码,但是我将有多个网格并试图减少代码。这就是我尝试动态执行此操作的原因。
    【解决方案3】:

    首先,命名&lt;DoubleAnimationUsingKeyFrames x:name="da1Load"&gt; 在后面的代码中:

    da1Load.SetValue(Storyboard.TargetNameProperty, "grdWithNewName")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-15
      • 2011-12-28
      • 2014-12-22
      • 2012-02-11
      • 2016-08-03
      • 1970-01-01
      • 1970-01-01
      • 2017-11-16
      相关资源
      最近更新 更多