C# - 阻止代码在设计器中执行

时间:2021-09-12 20:40:50

I'm executing a line of code that fails to execute within the designer, causing all of my control's public properties to no longer show up in the designer. Because of this I can no longer see any of my forms that use that control in the Visual Studios Design View.

我正在执行一行无法在设计器中执行的代码,导致我的所有控件的公共属性不再显示在设计器中。因此,我无法再在Visual Studios Design View中看到任何使用该控件的表单。

The line of code in question calls an unsafe code project that does a bit of image processing; commenting it out makes the design view come back to life. However, the code executes perfectly, so I cannot see why the code fails in the designer. This is the code that is being called:

有问题的代码行调用一个不安全的代码项目,它执行一些图像处理;评论它使设计视图重现生机。但是,代码执行完美,所以我无法理解设计器中代码失败的原因。这是被调用的代码:

        /// <summary>
        /// Performs a color adjustment on the source bitmap. 
        /// </summary>
        /// <param name="source">The bitmap to be processed. This is changed by the
        /// process action.</param>
        /// <param name="redChange">change to the red value. Can be negative.</param>
        /// <param name="greenChange">change to the green value. Can be negative.</param>
        /// <param name="blueChange">change to the blue value. Can be negative.</param>
        /// <returns></returns>
        public static Bitmap ProcessColor(Bitmap source, int redChange, int greenChange, int blueChange)
        {
            sourceBitmap = source;

            // lock the source bitmap
            sourceBitmapData = getBitmapData(sourceBitmap, ref sourceWidth);
            sourcepBase = (Byte*)sourceBitmapData.Scan0.ToPointer();

            PixelData* pPixel;

            for (int y = 0; y < source.Height; y++)
            {
                pPixel = (PixelData*)(sourcepBase + y * sourceWidth);
                for (int x = 0; x < source.Width; x++)
                {
                    int redVal = pPixel->red + redChange;
                    if ( redVal <0 ) redVal = 0;
                    if ( redVal > 255) redVal = 255;
                    pPixel->red = (byte)redVal;

                    int greenVal = pPixel->green + greenChange;
                    if ( greenVal <0 ) greenVal = 0;
                    if ( greenVal > 255) greenVal = 255;
                    pPixel->green = (byte)greenVal;

                    int blueVal = pPixel->blue + blueChange;
                    if (blueVal < 0) blueVal = 0;
                    if (blueVal > 255) blueVal = 255;
                    pPixel->blue = (byte)blueVal;

                    pPixel++;
                }
            }

            sourceBitmap.UnlockBits(sourceBitmapData);
            sourceBitmapData = null;
            sourcepBase = null;

            return source;
        }

(Courtesy of the OpenNETCF Community)

(由OpenNETCF社区提供)

My project is not marked as unsafe, but the project that the above code is in is marked unsafe. Do both projects need to be marked unsafe?

我的项目没有标记为不安全,但上面代码所在的项目标记为不安全。这两个项目都需要标记为不安全吗?

Or, is there a way I can block that line of code firing in the designer (I don't actually need the output of this code whilst in design view as it's just generating a disabled version of an image from a provided image).

或者,有没有办法可以阻止设计器中触发的代码行(在设计视图中我实际上不需要输出此代码,因为它只是从提供的图像生成图像的禁用版本)。

EDIT: Blocking the code from running does not fix the issue. Only commenting the line out allows the design view to work. Having the line in (even when put in an if[false == true] statement) causes the designer to display errors, instead of the form.

编辑:阻止代码运行不能解决问题。只有注释掉线才能使设计视图起作用。将行放入(即使放入if [false == true]语句中)会导致设计器显示错误,而不是表单。

2 个解决方案

#1


Wrap the part of code in if:

在以下情况下包装部分代码:

if(!DesignMode)
{
    // Your "unsafe" code goes here
}

If you're using Compact Framework, use this:

如果您使用的是Compact Framework,请使用以下命令:

if(Site != null && !Site.DesignMode)
{
    // Your "unsafe" code goes here
}

See this post on mode info on what a beast DesignMode actually is.

有关野兽DesignMode究竟是什么的模式信息,请参阅此文章。

#2


I did not create this, but thought I would add a useful trick found -->here<--.

我没有创建这个,但我想我会添加一个有用的技巧 - >这里< - 。

Just add this to your control and use it appropriately.

只需将其添加到您的控件并适当使用它。

public static bool IsDesignMode
{
  get 
  { 
    return AppDomain.CurrentDomain.FriendlyName.Contains("DefaultDomain"); 
  }
}

Or for us VB.NET folks:

或者对我们来说VB.NET人员:

  Public Shared ReadOnly Property IsDesignMode() As Boolean
    Get
      Return AppDomain.CurrentDomain.FriendlyName.Contains("DefaultDomain")
    End Get
  End Property

#1


Wrap the part of code in if:

在以下情况下包装部分代码:

if(!DesignMode)
{
    // Your "unsafe" code goes here
}

If you're using Compact Framework, use this:

如果您使用的是Compact Framework,请使用以下命令:

if(Site != null && !Site.DesignMode)
{
    // Your "unsafe" code goes here
}

See this post on mode info on what a beast DesignMode actually is.

有关野兽DesignMode究竟是什么的模式信息,请参阅此文章。

#2


I did not create this, but thought I would add a useful trick found -->here<--.

我没有创建这个,但我想我会添加一个有用的技巧 - >这里< - 。

Just add this to your control and use it appropriately.

只需将其添加到您的控件并适当使用它。

public static bool IsDesignMode
{
  get 
  { 
    return AppDomain.CurrentDomain.FriendlyName.Contains("DefaultDomain"); 
  }
}

Or for us VB.NET folks:

或者对我们来说VB.NET人员:

  Public Shared ReadOnly Property IsDesignMode() As Boolean
    Get
      Return AppDomain.CurrentDomain.FriendlyName.Contains("DefaultDomain")
    End Get
  End Property