找不到名称InvalidOperationException [英] Name cannot Found InvalidOperationException

查看:73
本文介绍了找不到名称InvalidOperationException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到异常,即在设置DoubleAnimation的StoryBoard.TargetName时找不到动态设置的椭圆名称....
我的XAML中已经有了StoryBoard ...

Im getting Exception that name of ellipse which is dynamically set is not found while setting StoryBoard.TargetName of DoubleAnimation....
I already have StoryBoard in my XAML...

public void CreateRandomStars()
       {

           int Width = (int)MyCanvas.Width;
           int Height = (int)MyCanvas.Height;


           Random Rnx = new Random(0);

           for (int i = 0; i < 300; i++)
           {

               double Lx = (double)Rnx.Next(Width);
               double Ty = (double)Rnx.Next(Height);
               starPoints.Add(Ty);
               Ellipse es = new Ellipse();
               es.Name = "es_" + i.ToString(); //setting names dynamically for each
               //ellipse

               es.Width = 2;
               es.Height = 2;

               es.Fill = Brushes.WhiteSmoke;
               es.SetValue(Canvas.LeftProperty, Lx);
               es.SetValue(Canvas.TopProperty, Ty);

               MyCanvas.Children.Add(es);

               DoubleAnimation dAnim = new DoubleAnimation();
               dAnim.From = Ty;
               dAnim.To = Height;
               dAnim.Duration = new Duration(new TimeSpan(0, 0, 5));
               dAnim.SetValue(Storyboard.TargetNameProperty, es.Name); //Getting Error
               dAnim.SetValue(Storyboard.TargetPropertyProperty, new PropertyPath(Canvas.TopProperty));

               MyStoryBoard.Children.Add(dAnim);

           }


       }



在"System.Windows.Controls.Canvas"的名称范围中找不到{''es_0"名称.}
这是错误...

XAML代码



{"''es_0'' name cannot be found in the name scope of ''System.Windows.Controls.Canvas''."}
This is the error...

XAML code

<Canvas Name="MyCanvas" Background="Black" Height="310" Width="500">
        <Canvas.Triggers>
            <EventTrigger RoutedEvent="Loaded">
                <BeginStoryboard>
                    <Storyboard RepeatBehavior="Forever" Name="MyStoryBoard">
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger>
        </Canvas.Triggers>
    </Canvas>

推荐答案

我将您的XAML更改为以下,并且没有任何异常,并且出现了星星.

I changed your XAML to below and I didn''t got any exception and the stars showed up.

<EventTrigger RoutedEvent="Canvas.Loaded">



在这里,我从MyCanvas''s Loaded事件调用CreateRandomStars()方法.

但是,当从Window''s Loaded事件调用CreateRandomStars()方法时,我收到了错误消息.这很容易理解.触发Window''s Loaded事件时,画布中的子级尚未准备好.因此,请在MyCanvas''s Loaded事件中调用该方法,并如上所述更改XAML.

将其标记为答案(如果有帮助的话).



Here I am calling CreateRandomStars() method from MyCanvas''s Loaded event.

But when CreateRandomStars() method is called from Window''s Loaded event, I am getting the error message. It is simple to understand. The Children in the Canvas is not ready when Window''s Loaded event is fired. So, call the method in MyCanvas''s Loaded event and change the XAML as I mentioned above.

Mark it as answer, if it is helpful.


您需要先注册该名称,然后才能将其用于任何其他控件.

You need to Register the name before you use it for any other control.

public void CreateRandomStars()
        {

            int Width = (int)MyCanvas.Width;
            int Height = (int)MyCanvas.Height;


            Random Rnx = new Random(0);

            for (int i = 0; i < 300; i++)
            {

                double Lx = (double)Rnx.Next(Width);
                double Ty = (double)Rnx.Next(Height);
                //starPoints.Add(Ty);
                Ellipse es = new Ellipse();
                es.Name = "es_" + i.ToString(); //setting names dynamically for each
                //ellipse

                es.Width = 2;
                es.Height = 2;

                es.Fill = Brushes.WhiteSmoke;
                es.SetValue(Canvas.LeftProperty, Lx);
                es.SetValue(Canvas.TopProperty, Ty);

                MyCanvas.Children.Add(es);

                this.RegisterName(es.Name, es);

                DoubleAnimation dAnim = new DoubleAnimation();
                dAnim.From = Ty;
                dAnim.To = Height;
                dAnim.Duration = new Duration(new TimeSpan(0, 0, 5));
                Storyboard.SetTargetName(dAnim, es.Name);
                Storyboard.SetTargetProperty(dAnim, new PropertyPath(Canvas.TopProperty));
                //StoryBoard.SetValue(Storyboard.TargetNameProperty, es.Name); //Getting Error
                //dAnim.SetValue(Storyboard.TargetPropertyProperty, new PropertyPath(Canvas.TopProperty));

                MyStoryBoard.Children.Add(dAnim);

            }


        }



当您希望其他对象引用动态创建的元素时,RegisterName是必需的.

我希望这对您有用.



The RegisterName is necessary when you want other object to refer your dynamically created element.

I hope this will work for you.


这篇关于找不到名称InvalidOperationException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆