Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 选中以避免在WPF/C中向画布添加多个项目# Shape Shape=sm.maakEllips(); 如果(!canvas.Children.Contains(shape)) { cm.绘制(形状、画布、位置); } 公共虚空绘制(形状vorm、画布、点位置) { 如果(vorm.Height_C#_Wpf_Canvas - Fatal编程技术网

C# 选中以避免在WPF/C中向画布添加多个项目# Shape Shape=sm.maakEllips(); 如果(!canvas.Children.Contains(shape)) { cm.绘制(形状、画布、位置); } 公共虚空绘制(形状vorm、画布、点位置) { 如果(vorm.Height

C# 选中以避免在WPF/C中向画布添加多个项目# Shape Shape=sm.maakEllips(); 如果(!canvas.Children.Contains(shape)) { cm.绘制(形状、画布、位置); } 公共虚空绘制(形状vorm、画布、点位置) { 如果(vorm.Height,c#,wpf,canvas,C#,Wpf,Canvas,您的Draw()方法将vorm类型Shape添加到canvas中指定的画布中。我假设您的sm.maakEllips()返回一个椭圆 因此,当您运行以下代码时: Shape shape = sm.makeShape(Convert.ToByte(textboxR.Text), Convert.ToByte(textboxG.Text), Convert.ToByte(textboxB.Text), Convert.ToInt32(textboxHoogte.Text), C

您的
Draw()
方法将
vorm
类型
Shape
添加到
canvas
中指定的画布中。我假设您的
sm.maakEllips()
返回一个椭圆

因此,当您运行以下代码时:

            Shape shape = sm.makeShape(Convert.ToByte(textboxR.Text), Convert.ToByte(textboxG.Text), Convert.ToByte(textboxB.Text), Convert.ToInt32(textboxHoogte.Text), Convert.ToInt32(textboxBreedte.Text));
            foreach (Shape existingShape in canvas.Children.OfType<Shape>())
            {
                if (existingShape.Width != shape.Width && existingShape.Height != shape.Height
                    && existingShape.Fill != shape.Fill)
                {
                    cm.Draw(shape, canvas, locatie);
                }
            }
如果画布包含您在上行中使用
sm.maakEllips()创建的确切
shape
对象,您将进入
if
语句
方法。它不能是任何具有上述
形状
对象相同属性的形状。因为,每次创建新对象时,即使包括其名称在内的属性完全相同,它们仍然是
.NET
世界中两个截然不同的对象

要说明这一点,请参见下面的代码示例

您未更改的
Draw()
方法:

Shape shape = sm.maakEllips();
if (!canvas.Children.Contains(shape))
{
    cm.Draw(shape, canvas, locatie);
}
现在查看以下代码,单击按钮即可执行

public Shape makeEllipse(string name)
{
    Shape sh = new Ellipse
    {
        Name = name,
        Width = 100,
        Height = 80,
    };
    return sh;
}
上面的评论应该足够好,但要再次解释

  • 当您使用
    Draw()
    方法创建
    sh1
    并将其添加到
    myCanvas
    时,它将成为
    myCanvas.Children
    的子元素
  • 然后,当您使用
    if(!myCanvas.Children.Contains(sh1))
    检查它是否为子元素时,由于到那时它是子元素,因此条件变为
    false
    ,我们不进入
    if
    子句
  • 接下来,我们创建
    sh2
    ,它的维度和名称与
    sh1
    完全相同。然而,这是关键,
    .NET
    将它视为一个不同的对象,即使它与前一个对象具有相同的属性。原因是,每当我们使用
    new
    关键字时,
    .NET
    会创建一个实际的网元w反对
  • 之后,我们不会使用
    Draw()
    方法将其添加到画布
  • 现在,在第二个
    if
    中,当我们检查
    myCanvas
    是否包含该对象时,它发现
    sh2
    不是
    myCanvas
    的子对象,因此它进入了
    if
    子句中
您的
Draw()
方法将类型为
Shape
vorm
添加到
canvas
中指定的画布中。我假设您的
sm.maakEllips()
返回一个椭圆

因此,当您运行以下代码时:

            Shape shape = sm.makeShape(Convert.ToByte(textboxR.Text), Convert.ToByte(textboxG.Text), Convert.ToByte(textboxB.Text), Convert.ToInt32(textboxHoogte.Text), Convert.ToInt32(textboxBreedte.Text));
            foreach (Shape existingShape in canvas.Children.OfType<Shape>())
            {
                if (existingShape.Width != shape.Width && existingShape.Height != shape.Height
                    && existingShape.Fill != shape.Fill)
                {
                    cm.Draw(shape, canvas, locatie);
                }
            }
如果画布包含您在上行中使用
sm.maakEllips()创建的确切
shape
对象,您将进入
if
语句
方法。它不能是任何具有上述
形状
对象相同属性的形状。因为,每次创建新对象时,即使包括其名称在内的属性完全相同,它们仍然是
.NET
世界中两个截然不同的对象

要说明这一点,请参见下面的代码示例

您未更改的
Draw()
方法:

Shape shape = sm.maakEllips();
if (!canvas.Children.Contains(shape))
{
    cm.Draw(shape, canvas, locatie);
}
现在查看以下代码,单击按钮即可执行

public Shape makeEllipse(string name)
{
    Shape sh = new Ellipse
    {
        Name = name,
        Width = 100,
        Height = 80,
    };
    return sh;
}
上面的评论应该足够好,但要再次解释

  • 当您使用
    Draw()
    方法创建
    sh1
    并将其添加到
    myCanvas
    时,它将成为
    myCanvas.Children
    的子元素
  • 然后,当您使用
    if(!myCanvas.Children.Contains(sh1))
    检查它是否为子元素时,由于到那时它是子元素,因此条件变为
    false
    ,我们不进入
    if
    子句
  • 接下来,我们创建
    sh2
    ,它的维度和名称与
    sh1
    完全相同。然而,这是关键,
    .NET
    将它视为一个不同的对象,即使它与前一个对象具有相同的属性。原因是,每当我们使用
    new
    关键字时,
    .NET
    会创建一个实际的网元w反对
  • 之后,我们不会使用
    Draw()
    方法将其添加到画布
  • 现在,在第二个
    if
    中,当我们检查
    myCanvas
    是否包含该对象时,它发现
    sh2
    不是
    myCanvas
    的子对象,因此它进入了
    if
    子句中
    • maakEllips()
      始终创建一个新的
      形状
      。如果要将此形状与
      画布
      中的其他
      形状
      元素进行比较,则需要迭代这些元素。以下代码比较高度、宽度和位置,并将新的
      形状
      添加到
      画布
      (如果其中任何一个不同):

      private void btnGO_Click(object sender, RoutedEventArgs e)
      {
          // Creates an ellipse with name "Shape1", and assigns to sh1.
          Shape sh1 = makeEllipse("Shape1");
      
          // Adds the said sh1 to the canvas using `Draw()` method.
          Draw(sh1, myCanvas, new Point(5, 5));
      
          // See if sh1 exists as a child of `myCanvas`.
          // Since sh1 is now a child of canvas, code does NOT go inside the if-clause.
          if (!myCanvas.Children.Contains(sh1))
          {
              Draw(sh1, myCanvas, new Point(5, 5));
          }
      
          // Creates an ellipse with the same name "Shape1", and assigns to sh2.
          Shape sh2 = makeEllipse("Shape1");
      
          // It is NOT added to the canvas using `Draw()` method.
      
          // Now, here, code DOES go inside the if-clause, because the said object does not exist as a child of `myCanvas`.
          if (!myCanvas.Children.Contains(sh2))
          {
              Draw(sh2, myCanvas, new Point(5, 5));
          }
      }
      
      Shape Shape=sm.maakEllips();
      foreach(图形存在canvas.Children.OfType()中的图形)
      {
      如果(existingShape.Width!=画布.Width | | existingShape.Height!=画布.Height
      ||Canvas.GetLeft(现有形状)!=Canvas.GetLeft(形状)
      ||Canvas.GetTop(现有形状)!=Canvas.GetTop(形状))
      {
      cm.绘制(形状、画布、位置);
      }
      }
      
      maakEllips()
      始终创建一个新的
      形状
      。如果要将此形状与
      画布
      中的其他
      形状
      元素进行比较,则需要迭代这些元素。以下代码比较高度、宽度和位置,并将新的
      形状
      添加到
      画布
      (如果其中任何一个不同):

      private void btnGO_Click(object sender, RoutedEventArgs e)
      {
          // Creates an ellipse with name "Shape1", and assigns to sh1.
          Shape sh1 = makeEllipse("Shape1");
      
          // Adds the said sh1 to the canvas using `Draw()` method.
          Draw(sh1, myCanvas, new Point(5, 5));
      
          // See if sh1 exists as a child of `myCanvas`.
          // Since sh1 is now a child of canvas, code does NOT go inside the if-clause.
          if (!myCanvas.Children.Contains(sh1))
          {
              Draw(sh1, myCanvas, new Point(5, 5));
          }
      
          // Creates an ellipse with the same name "Shape1", and assigns to sh2.
          Shape sh2 = makeEllipse("Shape1");
      
          // It is NOT added to the canvas using `Draw()` method.
      
          // Now, here, code DOES go inside the if-clause, because the said object does not exist as a child of `myCanvas`.
          if (!myCanvas.Children.Contains(sh2))
          {
              Draw(sh2, myCanvas, new Point(5, 5));
          }
      }
      
      Shape Shape=sm.maakEllips();
      foreach(图形存在canvas.Children.OfType()中的图形)
      {
      如果(existingShape.Width!=画布.Width | | existingShape.Height!=画布.Height
      ||Canvas.GetLeft(现有形状)!=Canvas.GetLeft(形状)
      ||Canvas.GetTop(现有形状)!=Canvas.GetTop(形状))
      {
      cm.绘制(形状、画布、位置);
      }
      }
      
      当您创建一个尚未添加到画布的新形状时,它将永远不会是画布的子图形。或者您是