Powered by MSDN

US - English
NEW! Silverlight 5 is available Learn More

Object creation by using xaml fails RSS

13 replies

Last post Jun 25, 2008 12:10 PM by ivanatilca

(0)
  • ivanatilca

    ivanatilca

    Member

    113 Points

    110 Posts

    Re: Object creation by using xaml fails

    Jun 25, 2008 04:41 AM | LINK

    ssawchenko

    It may be because you are not giving the resources names?  I thought that with Silverlight 1.1 and above, all elements in a ResourceDictionary [including Storyboard] must have their x:Name set... I may be wrong though, I'll have to double check with the actual code tomorrow.

    Nop, "THIS" is refering to my USERCONTROL i am trying to add to my resources an animation i am creating in that case is the storyboard.... the thing is the Foreach ... i believe it is giving the same name to all my storyboard an can not give the same name ... but that's why i created the INDEX so i can create my names... or keys.... but even like that the error appears-.

     

    Ivana A. Tilca | Microsoft Student Partner | Channel 8 editor | Microsoft

    http://ivanatilca.com.ar
  • ivanatilca

    ivanatilca

    Member

    113 Points

    110 Posts

    Re: Re: Object creation by using xaml fails

    Jun 25, 2008 04:43 AM | LINK

    SteveWong

    ivanatilca: 

    What is the error giving?

    I cant see any problem with the lines

    But If i were you, I will write this

                var u = from element in LayoutRoot.Children
                        where element is UIElement
                        select element;
                foreach (var itm in u)
                {
                    myDoubleAnimation = new DoubleAnimation();
                    myDoubleAnimation.Duration = duration;
                    myDoubleAnimation.From = 0;

                    sbImageAppear = new Storyboard();
                    sbImageAppear.Duration = duration;
                    sbImageAppear.Children.Add(myDoubleAnimation);               

                    TimeSpan time = new TimeSpan(0, 0, 0, 0, timeCounter);
                    sbImageAppear.BeginTime = time;

                    Storyboard.SetTarget(myDoubleAnimation, ((UIElement)itm));
                    Storyboard.SetTargetProperty(myDoubleAnimation, new PropertyPath("(UIElement.Opacity)"));
                    myDoubleAnimation.To = opacity;              
                    this.Resources.Add(index.ToString(), sbImageAppear); 
                    sbImageAppear.Begin();
                    timeCounter = timeCounter + 200;
                    index++;
                }

     The error is in the line... this.Resources.Add(index.ToString(), sbImageAppear);  says i already have that key...

    Ivana A. Tilca | Microsoft Student Partner | Channel 8 editor | Microsoft

    http://ivanatilca.com.ar
  • SteveWong

    SteveWong

    Contributor

    6769 Points

    1351 Posts

    Re: Re: Re: Object creation by using xaml fails

    Jun 25, 2008 04:58 AM | LINK

    If you do the second time, it is sure that a storyboard with the same key had been added

    So you have to remove it before adding it

    this.Resources.Remove(index.ToString());

    As I remember , It shouldnt throw any error even the key is not there

    Regards,
    SteveWong (HongKong)
    Please mark post as answer if they help you
  • ivanatilca

    ivanatilca

    Member

    113 Points

    110 Posts

    Re: Re: Re: Object creation by using xaml fails

    Jun 25, 2008 12:10 PM | LINK

    SteveWong

    If you do the second time, it is sure that a storyboard with the same key had been added

    So you have to remove it before adding it

    this.Resources.Remove(index.ToString());

    As I remember , It shouldnt throw any error even the key is not there

    Yes! That's it! Thanks!!!! The final code....

    if (this.Resources.Contains(index.ToString()))
    {
                        this.Resources.Remove(index.ToString());
     }
    this.Resources.Add(index.ToString(), sbImageAppear);

    Ivana A. Tilca | Microsoft Student Partner | Channel 8 editor | Microsoft

    http://ivanatilca.com.ar