8

此处描述了一个示例。但作者显然忘记包含下载代码。

此处显示了另一个示例。但是,这个不太有效(如评论中所述)。

你如何正确地做到这一点?

4

3 回答 3

4

您发现的第二个示例几乎可以正常工作,只是缺少了一点。所需要的只是主控件中的 2 种方法。

将此代码添加到AppointmentControl.cs文件中,它将起作用。

protected override object SaveViewState()
{
    if (appointments != null)
        return appointments.SaveViewState();
    return null;
}

protected override void LoadViewState(object savedState)
{
    appointments = new AppointmentCollection();
    appointments.LoadViewState(savedState);
}

示例站点中的代码相当不错。它实现了它应该拥有的所有接口并且做得很好。它崩溃的地方在于,尽管在抽象位中有它需要的所有代码,但这并不重要,因为接口没有在它们需要的地方被引用。

除了实现一些接口之外,所使用的集合类没有什么“特别”之处。框架不会自动调用这些方法。然而,该框架调用我在上面编写的重写方法,您需要实现这些方法以便您的控件将元素保存在集合中。只要你打电话给他们,一切都会奏效。

于 2009-02-01T02:55:35.093 回答
3

丹赫伯特明白了。该死,我也花了几个小时在这上面!在尝试回答这个问题的过程中,我想出了一个简化的通用 StateManagedCollection,它继承自框架的内置 StateManagedCollection,基于此处的版本。也许你会发现它很有用。我的示例项目的完整源代码可在此处获得。

using System;
using System.Collections;
using System.Collections.Specialized;
using System.Security.Permissions;
using System.Web;
using System.Collections.Generic;
using System.Web.UI;

namespace Web
{
    public abstract class StateManagedCollection<T> : StateManagedCollection, IList<T>, ICollection<T>, IEnumerable<T>
        where T : class, IStateManagedItem, new()
    {

        protected override object CreateKnownType(int index)
        {
            return Activator.CreateInstance<T>();
        }

        protected override Type[] GetKnownTypes()
        {
            return new Type[] { typeof(T) };
        }

        protected override void SetDirtyObject(object o)
        {
            ((IStateManagedItem)o).SetDirty();
        }

        #region IList<T> Members

        public int IndexOf(T item)
        {
            return ((IList)this).IndexOf(item);
        }

        public void Insert(int index, T item)
        {
            ((IList)this).Insert(index, item);
            if (((IStateManager)this).IsTrackingViewState)
            {
                this.SetDirty();
            }
        }

        public void RemoveAt(int index)
        {
            ((IList)this).RemoveAt(index);
            if (((IStateManager)this).IsTrackingViewState)
            {
                this.SetDirty();
            }
        }

        public T this[int index]
        {
            get { return (T)this[index]; }
            set { this[index] = value; }
        }

        #endregion

        #region ICollection<T> Members

        public void Add(T item)
        {
            ((IList)this).Add(item);
            this.SetDirty();
        }

        public bool Contains(T item)
        {
            return ((IList)this).Contains(item);
        }

        public void CopyTo(T[] array, int arrayIndex)
        {
            ((IList)this).CopyTo(array, arrayIndex);
        }

        public bool IsReadOnly
        {
            get { return false; }
        }

        public bool Remove(T item)
        {
            if (((IList)this).Contains(item))
            {
                ((IList)this).Remove(item);
                return true;
            }
            return false;
        }

        #endregion


        #region IEnumerable<T> Members

        IEnumerator<T> IEnumerable<T>.GetEnumerator()
        {
            throw new NotImplementedException();
        }

        #endregion

        #region IEnumerable Members

        IEnumerator IEnumerable.GetEnumerator()
        {
            return ((IList)this).GetEnumerator();

        }

        #endregion
    }
}
于 2009-02-01T03:20:31.067 回答
0

我已经使用了上面提供的解决方案的代码,但是我得到了异常-“StackOverflow”很酷:) 通过在 aspx 页面中添加一些子项并切换到设计视图到 Visual Studio 的设计视图(Visual Studio 只是重新启动,没有人知道发生了什么......)。

    IEnumerator IEnumerable.GetEnumerator()
    {
        return ((IList)this).GetEnumerator();

    }

所以,我想我想通了只是像这样改变上述方法的实现:

    IEnumerator IEnumerable.GetEnumerator()
    {
        // return ((IList)this).GetEnumerator();
        return this.GetEnumerator();

    } 

希望这会帮助像我这样的其他人:) 只是不要浪费几个小时来让它与 VS 的设计师合作

于 2010-03-18T21:56:32.983 回答