0

一般的な呼び出しを使用してフォームにプロパティを設定する方法が必要です。一種のシングルトンフォームを作成する静的クラスがあり、プリンシパルフォーム(MDI)が呼び出しを行い、クラスが呼び出しを処理し、表示、開くなどのいずれかを認識します。

フォームにプロパティを設定する必要があることに気付いた瞬間まで、すべてが順調に進んでいました。私はそれを行うことができますが、フォームがロードされる前に割り当てが発生するようにしたいです。

私はこれを達成する方法を考え出しました、少なくとも私はクールなアイデアを持っていたと思いました、しかし...コードを見てみましょう:

    public interface IFormBase
    {
        Action<IFormBase> SetParameters { get; set; }
    }

    public class FormBase : Form, IFormBase
    {
        public Action<IFormBase> SetParameters { get; set; }

        protected override void OnLoad(EventArgs e)
        {
            if (SetParameters != null)
            {
                SetParameters.Invoke(this);
            }
            base.OnLoad(e);
        }
    }

...後でFormManager静的クラスで..。

        public TResult GetSingleTonForm<TResult, T>(object state, Action<T> setParameters)
            where TResult : FormBase
            where T : IFormBase
        {
            Type t = typeof(T);
            FormBase f = null;

            if (f == null)
            {
                f = (FormBase)Activator.CreateInstance(t);
            }

            if (setParameters != null && f is IFormBase)
            {
                f.SetParameters = setParameters;
            }

            return (TResult)f;
        }
...

問題:

System.Action<T>タイプを暗黙的に変換することはできませんSystem.Action<blabla.IFormBase>

エラーを理解しました。別の解決策を詳しく説明するために助けを求めています。ありがとう!

4

1 に答える 1

0

これはどうですか:

public T GetSingleTonForm<T>(object state, Action<T> setParameters)
            where T : FormBase, IFormBase 
        {
            Type t = typeof(T);
            FormBase f = null;
            if (f == null)
            {
                 f = (FormBase)Activator.CreateInstance(t);
            }

            if (setParameters != null && f is IFormBase)
            {
                setParameters.Invoke((T)f);
            }

            return (T)f;
        }

それはコンパイルされます、そして私はそれがあなたが必要とすることをすると思います。OnLoadこの方法では、あなたのどちらかで電話をかける必要はありませんFormBase

于 2012-10-11T16:54:22.787 に答える