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

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

        /// <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;
        }

(由OpenNETCF社区提供)

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

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

编辑:阻止代码运行并不能解决问题。仅对该行进行注释可以使设计视图起作用。将行放入(即使放入if [false == true]语句中)会导致设计人员显示错误,而不是表单。

有帮助吗?

解决方案

将部分代码包裹在if

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

如果您使用的是Compact Framework,请使用:

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

请参阅有关模式信息的这篇文章什么是野兽DesignMode实际上是。

其他提示

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

只需将其添加到您的控件中并正确使用它即可。

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

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

  Public Shared ReadOnly Property IsDesignMode() As Boolean
    Get
      Return AppDomain.CurrentDomain.FriendlyName.Contains("DefaultDomain")
    End Get
  End Property
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top