设计者必须创建一个实例... ...不能因为类型声明为抽象 [英] The designer must create an instance of...cannot because the type is declared abstract

查看:222
本文介绍了设计者必须创建一个实例... ...不能因为类型声明为抽象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Visual Studio中抱怨:警告1设计者必须创建类型RentalEase.CustomBindingNavForm的实例,但不能因为类型声明为抽象的

Visual Studio将不会让我访问设计表单。类已经实现了从CustomBindingNavForm所有的抽象方法。 CustomBindingNavForm提供了一些功能,具体与抽象。​​

有没有解决的办法吗?

下面是类:

 公共抽象类CustomBindingNavForm:SingleInstanceForm {

        //标志管理的BindingSource
        保护布尔isNew = FALSE;
        保护布尔isUpdating = FALSE;

        ///<总结>
        ///这是这样,当一个新的项目添加,它集isNew和firstPass为true。变化了的位置事件会寻找
        /// firstPass如果它是真实的。将其设置为false。然后在接下来的通行证,它会看到它是假的,并设置isNew为false。
        ///这是必要的,因为当一个新项目添加了改变位置的事件将火。
        ///< /总结>
        保护布尔firstPass = FALSE;


        保护抽象BOOL validateInput();
        保护抽象无效saveToDatabase();


        //操纵绑定
        保护抽象无效bindingSourceCancelResetCurrent();
        保护抽象无效bindingSourceRemoveCurrent();
        保护抽象无效bindingSourceMoveFirst();
        保护抽象无效bindingSourceMoveNext();
        保护抽象无效bindingSourceMoveLast();
        保护抽象无效bindingSourceMove previous();
        保护抽象无效bindingSourceAddNew();

        公共无效bindingNavigatorMove previousItem_Click(对象发件人,EventArgs的){
            如果(validateInput()){
                bindingSourceMove previous();
            } 其他 {
                DialogResult的CONT =的MessageBox.show(NULL,有你​​的数据错误。点击取消返回去解决这些问题,或确定继续。如果继续,更改将不会被保存。,是否继续?,MessageBoxButtons。艾伯塔省,MessageBoxIcon.Question,MessageBoxDefaultButton.Button2);
                如果(续== DialogResult.OK){
                    如果(isNew){
                        bindingSourceRemoveCurrent();
                        isNew = FALSE;
                    } 其他 {
                        bindingSourceCancelResetCurrent();
                        bindingSourceMove previous();
                    }
                }
            }
        }

        公共无效bindingNavigatorAddNewItem_Click(对象发件人,EventArgs的){
            如果(validateInput()){
                saveToDatabase();
                布尔TEMP = isUpdating;
                isUpdating = TRUE;
                bindingSourceAddNew();
                isUpdating =温度;

                isNew = TRUE;
                firstPass = TRUE;
            } 其他 {
                DialogResult的CONT =的MessageBox.show(NULL,有你​​的数据错误。点击取消返回去解决这些问题,或确定继续。如果继续,更改将不会被保存。,是否继续?,MessageBoxButtons。艾伯塔省,MessageBoxIcon.Question,MessageBoxDefaultButton.Button2);
                如果(续== DialogResult.OK){

                    如果(isNew){
                        bindingSourceRemoveCurrent();
                        isNew = FALSE;
                    } 其他 {
                        bindingSourceCancelResetCurrent();
                    }

                    布尔TEMP = isUpdating;
                    isUpdating = TRUE;
                    bindingSourceAddNew();
                    isUpdating =温度;

                    isNew = TRUE;
                    firstPass = TRUE;
                }
            }
        }

        公共无效bindingNavigatorMoveFirstItem_Click(对象发件人,EventArgs的){
            如果(validateInput()){
                bindingSourceMoveFirst();
            } 其他 {
                DialogResult的CONT =的MessageBox.show(NULL,有你​​的数据错误。点击取消返回去解决这些问题,或确定继续。如果继续,更改将不会被保存。,是否继续?,MessageBoxButtons。艾伯塔省,MessageBoxIcon.Question,MessageBoxDefaultButton.Button2);
                如果(续== DialogResult.OK){
                    如果(isNew){
                        bindingSourceRemoveCurrent();
                        isNew = FALSE;
                    } 其他 {
                        bindingSourceCancelResetCurrent();
                    }
                    bindingSourceMoveFirst();
                }
            }
        }

        公共无效bindingNavigatorMoveNextItem_Click(对象发件人,EventArgs的){
            如果(validateInput()){
                bindingSourceMoveNext();
            } 其他 {
                DialogResult的CONT =的MessageBox.show(NULL,有你​​的数据错误。点击取消返回去解决这些问题,或确定继续。如果继续,更改将不会被保存。,是否继续?,MessageBoxButtons。艾伯塔省,MessageBoxIcon.Question,MessageBoxDefaultButton.Button2);
                如果(续== DialogResult.OK){
                    如果(isNew){
                        bindingSourceRemoveCurrent();
                        isNew = FALSE;
                    } 其他 {
                        bindingSourceCancelResetCurrent();
                    }
                    bindingSourceMoveNext();
                }
            }
        }
    }
 

解决方案

我还没有看到在城市的马铃薯内容(其下行),但我和的 Smelch 想出了一个解决方案。 表格自己从一个抽象类继承,所以他们不告诉你的是,继承其唯一的1级,可以不是抽象的,就下来二日即可。

从那里有中间一个空的类和包装的#如果调试周围的形式声明,你去好了它的根本问题。只是一定要释放释放模式和设计在调试模式(这是很典型的)。

您会得到充分的设计师的支持和设计(调试)一​​个真正的抽象基类,并建立(释放)的时间,因为每一次使用抽象基类结束了。

<一个href="http://stackoverflow.com/questions/1620847/how-can-i-get-vs2008-winforms-designer-to-render-a-form-that-implements-an-abstra/2406058#2406058">The完整的解释和答案就在这里

Visual Studio complains: Warning 1 The designer must create an instance of type 'RentalEase.CustomBindingNavForm' but it cannot because the type is declared as abstract.

Visual Studio won't let me access the Designer for the form. The class already implements all abstract methods from the CustomBindingNavForm. CustomBindingNavForm provides some functions concrete and abstract.

Is there a way around this?

Here is the class:

 public abstract class CustomBindingNavForm : SingleInstanceForm {     

        //Flags for managing BindingSource
        protected bool isNew = false;
        protected bool isUpdating = false;

        /// <summary>
        /// This is so that when a new item is added, it sets isNew and firstPass to true. The Position Changed Event will look for
        /// firstPass and if it is true set it to false. Then on the next pass, it will see it's false and set isNew to false.
        /// This is needed because the Position Changed Event will fire when a new item is added.
        /// </summary>
        protected bool firstPass = false;


        protected abstract bool validateInput();
        protected abstract void saveToDatabase();


        //manipulating binding
        protected abstract void bindingSourceCancelResetCurrent();
        protected abstract void bindingSourceRemoveCurrent();
        protected abstract void bindingSourceMoveFirst();
        protected abstract void bindingSourceMoveNext();
        protected abstract void bindingSourceMoveLast();
        protected abstract void bindingSourceMovePrevious();
        protected abstract void bindingSourceAddNew();

        public void bindingNavigatorMovePreviousItem_Click(object sender, EventArgs e) {
            if (validateInput()) {
                bindingSourceMovePrevious();
            } else {
                DialogResult cont = MessageBox.Show(null, "There are errors in your data. Click Cancel to go back and fix them, or ok to continue. If you continue, changes will not be saved.", "Continue?", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
                if (cont == DialogResult.OK) {
                    if (isNew) {
                        bindingSourceRemoveCurrent();
                        isNew = false;
                    } else {
                        bindingSourceCancelResetCurrent();
                        bindingSourceMovePrevious();
                    }
                }
            }
        }

        public void bindingNavigatorAddNewItem_Click(object sender, EventArgs e) {
            if (validateInput()) {
                saveToDatabase();
                bool temp = isUpdating;
                isUpdating = true;
                bindingSourceAddNew();
                isUpdating = temp;

                isNew = true;
                firstPass = true;
            } else {
                DialogResult cont = MessageBox.Show(null, "There are errors in your data. Click Cancel to go back and fix them, or ok to continue. If you continue, changes will not be saved.", "Continue?", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
                if (cont == DialogResult.OK) {

                    if (isNew) {
                        bindingSourceRemoveCurrent();
                        isNew = false;
                    } else {
                        bindingSourceCancelResetCurrent();
                    }

                    bool temp = isUpdating;
                    isUpdating = true;
                    bindingSourceAddNew();
                    isUpdating = temp;

                    isNew = true;
                    firstPass = true;
                }
            }
        }

        public void bindingNavigatorMoveFirstItem_Click(object sender, EventArgs e) {
            if (validateInput()) {
                bindingSourceMoveFirst();
            } else {
                DialogResult cont = MessageBox.Show(null, "There are errors in your data. Click Cancel to go back and fix them, or ok to continue. If you continue, changes will not be saved.", "Continue?", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
                if (cont == DialogResult.OK) {
                    if (isNew) {
                        bindingSourceRemoveCurrent();
                        isNew = false;
                    } else {
                        bindingSourceCancelResetCurrent();
                    }
                    bindingSourceMoveFirst();
                }
            }
        }

        public void bindingNavigatorMoveNextItem_Click(object sender, EventArgs e) {
            if (validateInput()) {
                bindingSourceMoveNext();
            } else {
                DialogResult cont = MessageBox.Show(null, "There are errors in your data. Click Cancel to go back and fix them, or ok to continue. If you continue, changes will not be saved.", "Continue?", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
                if (cont == DialogResult.OK) {
                    if (isNew) {
                        bindingSourceRemoveCurrent();
                        isNew = false;
                    } else {
                        bindingSourceCancelResetCurrent();
                    }
                    bindingSourceMoveNext();
                }
            }
        }
    }

解决方案

I haven't seen the content at urban potato (its down) but Me and Smelch came up with a solution. Form itself inherits from an abstract class, so what they dont tell you is that its only the 1st level of inheritance that can't be abstract, the 2nd on down can.

From there its simply a matter of having an empty class in the middle and wrapping an #if debug around the forms declaration and you're good to go. Just be sure to release in release mode and design in debug mode (which is very typical).

You'll get full designer support and a real abstract base class at design (debug) and build (release) time because each time it ends up using your abstract base class.

The full explanation and answer is here

这篇关于设计者必须创建一个实例... ...不能因为类型声明为抽象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆