0

我使用以下类来填充组合框:

public class DamageTypeList
{
    static Begbil2Entities _DB = new Begbil2Entities();
    public static List<HUB_DamageTypes> _list = (from d in _DB.HUB_DamageTypes orderby d.DamageOrder select d).ToList(); 

    public static List<HUB_DamageTypes> TList
    {
        get
        {
            return _list;
        }
    }
 }

在 xaml 文件中,我像这样添加它:

<UserControl.Resources>
    <me:DamageTypeList  x:Key="DamageTypeList"/>

xaml 行创建错误(仅在设计时,它在运行时完美运行):

无法创建“DamageTypeList”的实例。C:\HUB\HUB\HubbCostOfferPage.xaml

我找到了一些建议来解决它:

if (!DesignerProperties.IsInDesignTool)

但是如何使用它来解决我的问题?

4

2 回答 2

3

您可以使用标志 DesignerProperties.IsInDesignTool 来防止创建数据库并在列表中使用硬编码实体。

public class DamageTypeList
{
    static Begbil2Entities _DB;
    public static List<HUB_DamageTypes> _list;

    public static Begbil2Entities DB
    {
        get
        {
            if(_DB == null && !DesignerProperties.IsInDesignTool)
                _DB = new Begbil2Entities();
            return _DB;
        }
    }

    public static List<HUB_DamageTypes> TList
    {
        get
        {
            if(_list == null)
            {
                if(!DesignerProperties.IsInDesignTool)
                    _list = (from d in DB.HUB_DamageTypes orderby d.DamageOrder select d).ToList(); 
                else
                    _list = new List<HUB_DamageTypes>(){
                        // Initialize it with hardcoded values
                    };
            }
            return _list;
        }
    }
 }

Before doing that, tough, I would investigate a little further what is the cause of the design-time exception, as @fhlamarche suggested. You can try to debug the design time execution, is not that hard. See this link.

于 2013-02-18T17:36:19.807 回答
1

设计器尝试调用默认构造函数,但您的类没有。
您只需要向您的类添加一个privateinternal默认构造函数。

于 2013-02-18T14:56:47.607 回答