代码之家  ›  专栏  ›  技术社区  ›  Jacob Saylor

无法解析TargetName-Silverlight4 C#

  •  8
  • Jacob Saylor  · 技术社区  · 16 年前

    我得到了错误 无法解析TargetName grdgeGeneral . 我正在尝试做的是有一个淡出功能,它接受一个网格并将不透明度淡出到零。我在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();
            }
    

    我已经在一些教程网站和我的代码似乎遵循同样的顺序。我也参加过这个活动 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: 
    
    3 回复  |  直到 9 年前
        1
  •  3
  •   Phil    16 年前

    你真的不应该这样编写你的故事板,如果你不一定要这样做。一旦你的动画变得有点复杂,它会咬你。

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

    那应该会给你你想要的。

        2
  •  5
  •   John Conde    13 年前

    而不是使用

     Storyboard.SetTargetName(da, pGrid.Name);
    

    尝试

     Storyboard.SetTarget(da, pGrid);
    
        3
  •  1
  •   Gabe    15 年前

    首先,说出 <DoubleAnimationUsingKeyFrames x:name="da1Load"> 在代码隐藏中:

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