Silverlight实战示例补充(数据集模拟挂接事件)

Silverlight实战示例补充(数据集模拟挂接事件),第1张

概述这里是部分代码,其它代码见Silverlight实战示例,直接上代码: 1)DynamicDataField.cs     [Serializable]     public partial class DynamicDataField     {         public string FieldName { get; set; }         public string StrValu

这里是部分代码,其它代码见Silverlight实战示例,直接上代码:

1)Dynamicdatafield.cs
    [Serializable]
    public partial class Dynamicdatafield
    {
        public string FIEldname { get; set; }
        public string StrValue { get; set; }
        public DateTime DTValue { get; set; }
        public Byte[] ByteArrayValue { get; set; }
        public string DataType { get; set; }
        //增加一个脏标志
        public bool IsDirty { get; set; }
    }

2)Dynamicdatafield.Shared.cs
public partial class Dynamicdatafield
    {
        public object oldValue { get; set; }
        /// <summary>
        /// 用object在绑定时没问题,但不能双向赋值,所以更新不采用这种方式.
        /// </summary>
        public object Value
        {
            get
            {
                if (this.DataType == "datatime")
                {
                    return this.DTValue;
                }
                if (this.DataType == "byte[]")
                {
                    return this.ByteArrayValue;
                }
                return this.StrValue;
            }
            set
            {
                object theoldValue = null;
                if (this.DataType == "datatime")
                {
                    theoldValue = DTValue;
                    DTValue = (DateTime) value;
                }
                else if (this.DataType == "byte[]")
                {
                    theoldValue = this.ByteArrayValue;
                    this.ByteArrayValue = (byte[])value;
                }
                else
                {
                    this.StrValue = value.ToString();
                    theoldValue = this.StrValue;
                }
                oldValue = theoldValue;
                IsDirty = true;
                if (datafieldChanged != null)
                {
                    datafieldChanged(this,oldValue,value);
                }
                if (DataMemberChanged != null)
                {
                    DataMemberChanged("Value");
                }
            }
        }
        public MyRaiseDataMemberChanged DataMemberChanged {get;set;}
        //数据字段发生改变时的事件触发,用事件也可以。
        public datafieldChanged datafieldChanged { get; set; }
    }
    //定义委托用于事件
    public delegate voID MyRaiseDataMemberChanged(string propertyname);
    //数据改变时的委托定义
    public delegate voID datafieldChanged(Dynamicdatafield FIEld,object oldValue,object NewValue);
    public enum RowState
    {
        NoChanging,
        Changed,
        Inserted,
        Deleted
    }

3) DynamicdaTarow.Shared.cs
 public partial class DynamicdaTarow
    {
        public Dynamicdatafield this[string FIEldname]
        {
            get
            {
                Dynamicdatafield theFIEld = null;
                foreach (var fld in datafields)
                {
                    if (fld.FIEldname == FIEldname)
                    {
                        theFIEld = fld;
                        break;
                    }
                }
                return theFIEld;
            }
        }
        public Dynamicdatafield this[int Index]
        {
            get
            {
                return datafields[Index];
            }
        }
        public voID AcceptChanged()
        {
            foreach (var item in this.datafields)
            {
                item.IsDirty = false;
                item.oldValue = item.Value;
            }
        }
        //判断行中的字段是否已经改变.
        private bool RowHasChanged()
        {
            foreach (var item in this.datafields)
            {
                if (item.IsDirty == true)
                {
                    return true;
                }
            }
            return false;
        }
        //本来可以直接调用table的相关事件,但由于ComplexObject不能共享实例,以及维持类似Datatable和datafield,DaTarow的关系
        //不能穿越到客户端,所以这里采用比较笨的办法。主要提供给事件挂接.
        internal voID SetFIEldEvent(datafieldChanged FldEvent)
        {
            foreach (var item in this.datafields)
            {
                item.datafieldChanged = FldEvent;
            }
            this.datafieldChanged = FldEvent;
        }
        public datafieldChanged datafieldChanged;
        private RowState _RowState;
        public RowState RowState {
            get
            {
                if (_RowState == RowState.NoChanging)
                {
                    if (RowHasChanged() == true)
                    {
                        _RowState = RowState.Changed;
                    }
                }
                return _RowState;
            }
            set
            {
                _RowState = value;
            }
        }
    }

4)Dynamicdatatable.Shared.cs
public partial class Dynamicdatatable
    {
        public DynamicdataColumn this[string FIEldname]
        {
            get
            {
                DynamicdataColumn theCol = null;
                foreach (var col in Columns)
                {
                    if (col.FIEldname == FIEldname)
                    {
                        theCol = col;
                        break;
                    }
                }
                return theCol;
            }
        }
        public DynamicdataColumn this[int Index]
        {
            get
            {
                return Columns[Index];
            }
        }
        public voID AcceptChanged()
        {
            foreach (var item in this.Rows)
            {
                item.RowState = RowState.NoChanging;
                item.AcceptChanged();
            }
        }
        /// <summary>
        /// 要增加行调用这个方法.
        /// </summary>
        /// <returns></returns>
        public DynamicdaTarow NewRow()
        {
            DynamicdaTarow Row = new DynamicdaTarow();
            Row.RowState = RowState.Inserted;
            Row.datafields = new List<Dynamicdatafield>();
            foreach (var col in this.Columns)
            {
                Dynamicdatafield theFld = new Dynamicdatafield();
                theFld.IsDirty = false;
                theFld.DataType = col.DataType;
                Row.datafields.Add(theFld);
            }
            return Row;
        }
        private datafieldChanged _datafieldChanged;
        public datafieldChanged datafieldChanged
        {
            get
            {
                return _datafieldChanged;
            }
            set
            {
                _datafieldChanged = value;
                //设置FIEld的事件.
                foreach (var row in this.Rows)
                {
                    row.SetFIEldEvent(value);
                }
            }
        }
      
    }

5)Dynamicdatafield_1.cs,这个文件放在RIA Service的客户端部分.

using System;
using System.Net;
using System.windows;
using System.windows.Controls;
using System.windows.documents;
using System.windows.Ink;
using System.windows.input;
using System.windows.Media;
using System.windows.Media.Animation;
using System.windows.Shapes;

namespace MEntitIEs
{
    /// <summary>
    /// 这个部分是Dynamicdatafield客户端扩展部分,非常关键
    /// 主要是实现Dynamicdatafield几个关键属性的部分方法.
    /// 如果是改变前得控制,大家可以实现相关Changing方法。
    /// </summary>
    public partial class Dynamicdatafield
    {
        partial voID OnCreated()
        {
            this.DataMemberChanged = new MyRaiseDataMemberChanged(this.RaiseDataMemberChanged);
        }
        /// <summary>
        ///
        /// </summary>
        partial voID OnByteArrayValueChanged()
        {
            //.....
        }


        partial voID OnDTValueChanged()
        {
            if (this.datafieldChanged == null)
            {
                return;
            }
            this.datafieldChanged(this,DTValue);
            this.oldValue = Value;
        }
        /// <summary>
        /// 值改变时的处理。
        /// </summary>
        partial voID OnStrValueChanged()
        {
            if (this.datafieldChanged == null)
            {
                return;
            }
            this.datafieldChanged(this,StrValue);
            this.oldValue = Value;
        }
    }
}

6)数据加载时要做处理,代码如下:

theS.GetDynamicdatatable("select * from EmployeeInfo ",op =>
                {
                    if (op.HasError == false)
                    {
                        if (op.Value != null)
                        {
                            //处理成默认值,追踪变化.
                            op.Value.AcceptChanged();
                        }
                        //DataSource = op.Value.Rows;
                        Datatable = op.Value;
                        //挂接事件
                        Datatable.datafieldChanged = new datafieldChanged((fld,oldv,newv) =>
                        {
                            MessageBox.Show(newv.ToString());
                        });
                    }
                    else
                    {
                        MessageBox.Show(op.ErrorMsg);
                    }
                },null);

7)数据绑定xaml示意:

<sdk:DataGrID x:name="CustomGrID" ItemsSource="{Binding Path=Datatable.Rows}" GrID.Row="0" autoGenerateColumns="False" >
            <sdk:DataGrID.Columns>
                <sdk:DataGrIDTextColumn CanUserReorder="True" Binding="{Binding Path=[EmployeeID].Value,Mode=TwoWay}" CanUserResize="True" CanUserSort="True" WIDth="auto" />
                <sdk:DataGrIDTextColumn CanUserReorder="True" Binding="{Binding Path=[Employeename].StrValue,Mode=TwoWay}" CanUserResize="True" CanUserSort="True" WIDth="auto" />
                <sdk:DataGrIDTextColumn CanUserReorder="True" Binding="{Binding Path=[EmployeeDesc].Value,Mode=TwoWay}" CanUserResize="True" CanUserSort="True" WIDth="auto" />
                <sdk:DataGrIDTextColumn CanUserReorder="True" Binding="{Binding Path=[EmployeeAge].Value,Mode=TwoWay}" CanUserResize="True" CanUserSort="True" WIDth="auto" />
            </sdk:DataGrID.Columns>
        </sdk:DataGrID>

 

后记:原来写的时候这个部分没有具体实现,今天补上,这里有个地方要特别声明,FIEld,Row,table之间的关系可以定义,但在服务端并不能挂接,否则会发生客户端调用错误,这个在后面的Silverlight 的notfound错误分析博文中有说。同样,事件也无法穿越,因此只能在客户端加载数据的时候进行。

 


原文链接: @L_502_0@ 总结

以上是内存溢出为你收集整理的Silverlight实战示例补充(数据集模拟挂接事件)全部内容,希望文章能够帮你解决Silverlight实战示例补充(数据集模拟挂接事件)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1069369.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存