0

我使用 SQLMetal.exe 生成了这个类。它在运行时是非常可绑定的,但是如果我在设计时使用这个类,我所有的设计时混合绑定都会被破坏。

我正在使用 MVVM-Light 框架,并且正在为 WP7 构建应用程序。

如果我为这个类提取一个接口,并创建一个实现这个接口的简单 POCO,并在我的设计时数据源中使用我的简单 poco,那么所有绑定都会生效。

这是由 SQLMetal.exe 生成的类。

[Table(Name="InspectionGroup")]
public partial class InspectionGroup : INotifyPropertyChanging, INotifyPropertyChanged, IInspectionGroup
{

    private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);

    private int _InspectionGroupId;

    private string _GroupName;

    private System.DateTime _DateCreated;

    private EntitySet<InspectionHeader> _InspectionHeaders;

    private EntitySet<InspectionPoint> _InspectionPoints;

    #region Extensibility Method Definitions
    partial void OnLoaded();
    partial void OnValidate(System.Data.Linq.ChangeAction action);
    partial void OnCreated();
    partial void OnInspectionGroupIdChanging(int value);
    partial void OnInspectionGroupIdChanged();
    partial void OnGroupNameChanging(string value);
    partial void OnGroupNameChanged();
    partial void OnDateCreatedChanging(System.DateTime value);
    partial void OnDateCreatedChanged();
    #endregion

    public InspectionGroup()
    {
        this._InspectionHeaders = new EntitySet<InspectionHeader>(new Action<InspectionHeader>(this.attach_InspectionHeaders), new Action<InspectionHeader>(this.detach_InspectionHeaders));
        this._InspectionPoints = new EntitySet<InspectionPoint>(new Action<InspectionPoint>(this.attach_InspectionPoints), new Action<InspectionPoint>(this.detach_InspectionPoints));
        OnCreated();
    }

    [Column(Storage = "_InspectionGroupId", AutoSync = AutoSync.OnInsert, DbType = "Int NOT NULL IDENTITY", IsPrimaryKey = true, IsDbGenerated = true)]
    public int InspectionGroupId
    {
        get
        {
            return this._InspectionGroupId;
        }
        set
        {
            if ((this._InspectionGroupId != value))
            {
                this.OnInspectionGroupIdChanging(value);
                this.SendPropertyChanging();
                this._InspectionGroupId = value;
                this.SendPropertyChanged("InspectionGroupId");
                this.OnInspectionGroupIdChanged();
            }
        }
    }

    [Column(Storage = "_GroupName", DbType = "NVarChar(100) NOT NULL", CanBeNull = false)]
    public string GroupName
    {
        get
        {
            return this._GroupName;
        }
        set
        {
            if ((this._GroupName != value))
            {
                this.OnGroupNameChanging(value);
                this.SendPropertyChanging();
                this._GroupName = value;
                this.SendPropertyChanged("GroupName");
                this.OnGroupNameChanged();
            }
        }
    }

    [Column(Storage = "_DateCreated", DbType = "DateTime NOT NULL")]
    public System.DateTime DateCreated
    {
        get
        {
            return this._DateCreated;
        }
        set
        {
            if ((this._DateCreated != value))
            {
                this.OnDateCreatedChanging(value);
                this.SendPropertyChanging();
                this._DateCreated = value;
                this.SendPropertyChanged("DateCreated");
                this.OnDateCreatedChanged();
            }
        }
    }

    [Association(Name = "FK_InspectionHeader_InspectionGroup", Storage = "_InspectionHeaders", ThisKey = "InspectionGroupId", OtherKey = "InspectionGroupId", DeleteRule = "CASCADE")]
    public EntitySet<InspectionHeader> InspectionHeaders
    {
        get
        {
            return this._InspectionHeaders;
        }
        set
        {
            this._InspectionHeaders.Assign(value);
        }
    }

    [Association(Name = "FK_InspectionPoint_InspectionGroup", Storage = "_InspectionPoints", ThisKey = "InspectionGroupId", OtherKey = "InspectionGroupId", DeleteRule = "CASCADE")]
    public EntitySet<InspectionPoint> InspectionPoints
    {
        get
        {
            return this._InspectionPoints;
        }
        set
        {
            this._InspectionPoints.Assign(value);
        }
    }

    public event PropertyChangingEventHandler PropertyChanging;

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void SendPropertyChanging()
    {
        if ((this.PropertyChanging != null))
        {
            this.PropertyChanging(this, emptyChangingEventArgs);
        }
    }

    protected virtual void SendPropertyChanged(String propertyName)
    {
        if ((this.PropertyChanged != null))
        {
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    private void attach_InspectionHeaders(InspectionHeader entity)
    {
        this.SendPropertyChanging();
        entity.InspectionGroup = this;
    }

    private void detach_InspectionHeaders(InspectionHeader entity)
    {
        this.SendPropertyChanging();
        entity.InspectionGroup = null;
    }

    private void attach_InspectionPoints(InspectionPoint entity)
    {
        this.SendPropertyChanging();
        entity.InspectionGroup = this;
    }

    private void detach_InspectionPoints(InspectionPoint entity)
    {
        this.SendPropertyChanging();
        entity.InspectionGroup = null;
    }
}
4

2 回答 2

1

刚刚遇到同样的问题 - 我相信 INotifyPropertyChanging 东西在设计时不受支持,因为这个界面旨在让数据库的生活更轻松。由于在设计时没有数据库,所以整个事情都失败了,不能让你在 Blend 中工作(或者在我的情况下是 VS xaml 可视化编辑器)

注释掉 PropertyChanging 实现解决了这个问题。当然,某种预处理器 IFDEF 会更好,但不知道那部分..我只是评论代码

于 2012-01-30T21:56:38.940 回答
0

因为在设计时没有调用数据库、异步服务等。使用 ViewModel 的IsInDesignModeStatic属性来确定您何时处于设计时模式或运行时模式。

在运行时,您调用您的服务来检索数据。在设计时,您可以模拟您的服务并将一些对象添加到您的集合中或自己创建数据上下文,以便您可以更好地设计您的 UI。然而,这并不是绝对必要的,而是“可混合性”通常指的。

Laurent 的网站上有一些样片,他的 MIX 10 视频也推动了这一点。

另请参阅这篇关于异步调用存在相同问题的帖子。

样本(Daniel的缩写,但常识:

MainViewModel()
{
   if(!IsInDesignMode)
   {   
      //pull data from service  
   }
}
于 2011-12-05T08:28:14.610 回答