2010-03-24 20 views
8

Recibo el error No puedo resolver TargetName grdGeneral. Lo que intento hacer es tener una función de fundido que acepte una cuadrícula y desvanezca la opacidad a cero. Tengo esta función llamada en MouseLeftButtonDown y se carga después de la xaml y la forma se ha cargado.No se puede resolver TargetName - Silverlight4 C#

Llamando al fundido de salida:

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

La función de fundido de salida:

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

he estado en un puñado de sitios tutorial y mi código parece seguir el mismo orden. También he estado en este stackoverflow question antes de decir repost. Esa pregunta tiene que ver con mutlipages y simplemente estoy tratando de comenzar una animación.

El Seguimiento de la pila

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: 
+0

Supuestamente un error dentro Silverlight4. Para aquellos interesados ​​en utilizar Phil y funciona mejor que tratar de encontrar el camino en el código. http://betaforums.silverlight.net/forums/p/23517/84087.aspx –

+0

Se metió en el mismo problema. Definitivamente necesito cosas creadas en tiempo de ejecución. ¿Cual es la solución? El enlace de arriba está roto. –

Respuesta

3

realidad, no debería tratar de código de sus guiones como este si no absolutamente necesario. Una vez que tus animaciones se vuelvan un poco complejas te morderá.

Debería hacer esto en xaml, preferiblemente usando Blend. Pruebe esto en su 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> 

En su código detrás, usted entonces invocarlo así:

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

que debe darle lo que estás buscando.

También sería mejor usar un botón en lugar de la imagen.

+1

Gracias por el código; sin embargo, tendré varias grillas e intentaré reducir el código. Es por eso que estaba tratando de hacer esto de forma dinámica. –

1

en primer lugar, el nombre de la <DoubleAnimationUsingKeyFrames x:name="da1Load"> en código detrás:

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

en lugar de utilizar

Storyboard.SetTargetName(da, pGrid.Name); 

tratar

Storyboard.SetTarget(da, pGrid); 
+0

¡Gracias, Eric! Esto funcionó a la perfección! :-) –

Cuestiones relacionadas