gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 09:00:37 25 4
gpt4 key购买 nike

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

有问题的代码行调用了一个不安全的代码项目,该项目进行了一些图像处理;将其注释掉会使设计 View 恢复生机。但是,代码执行完美,所以我不明白为什么代码在设计器中失败。这是被调用的代码:

        /// <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 社区提供)

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

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

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

最佳答案

将部分代码包裹在if中:

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

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

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

参见 this post关于野兽 DesignMode 实际上是什么的模式信息。

关于compact-framework - C# - 阻止代码在设计器中执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/823998/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com