C#-デザイナーでのコードの実行をブロックする
-
05-07-2019 - |
質問
デザイナー内で実行に失敗するコード行を実行しているため、コントロールのすべてのパブリックプロパティがデザイナーに表示されなくなりました。このため、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; here <!> 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