سؤال

أنا أقوم بتنفيذ سطر من التعليمات البرمجية التي تفشل في التنفيذ داخل المصمم ، مما تسبب في عدم ظهور جميع خصائص التحكم العامة الخاصة بي في المصمم. ولهذا السبب ، لم يعد بإمكاني رؤية أي من أشكالي التي تستخدم التحكم في طريقة عرض تصميم الاستوديوهات البصرية.

يطلق سطر الكود المعني مشروع رمز غير آمن يقوم ببعض معالجة الصور ؛ التعليق عليه يجعل منظر التصميم يعود إلى الحياة. ومع ذلك ، فإن الرمز ينفذ تمامًا ، لذلك لا يمكنني معرفة سبب فشل الكود في المصمم. هذا هو الكود الذي يسمى:

        /// <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)

لم يتم تمييز مشروعي على أنه غير آمن ، لكن المشروع الذي تم فيه علامة الرمز أعلاه غير آمن. هل يحتاج كلا المشروعين إلى وضع علامة غير آمنة؟

أو ، هل هناك طريقة يمكنني من خلالها حظر هذا السطر من إطلاق الرمز في المصمم (لا أحتاج فعليًا إلى إخراج هذا الرمز أثناء عرض التصميم لأنه يولد نسخة معطلة من صورة من صورة مقدمة).

تحرير: منع الكود من التشغيل لا يحدد المشكلة. يتيح التعليق فقط على الخط Out لعرض التصميم للعمل. وجود السطر في (حتى عند وضعه في عبارة if [false == true]) يؤدي إلى عرض المصمم أخطاء ، بدلاً من النموذج.

هل كانت مفيدة؟

المحلول

لف جزء الكود في if:

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

إذا كنت تستخدم إطار عمل مدمج ، استخدم هذا:

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

نرى هذا المشنور على معلومات الوضع حول ما الوحش DesignMode في الواقع.

نصائح أخرى

لم أقم بإنشاء هذا ، لكنني اعتقدت أنني سأضيف خدعة مفيدة -> هنا <-.

فقط أضف هذا إلى سيطرتك واستخدمه بشكل مناسب.

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