WPF中的默认窗口实例

时间:2016-05-15 11:10:25

标签: c# wpf vb.net

我有Form1的申请,即注册表格,Form2是实际申请。我需要在注册应用程序时,不再打开Form1而不仅仅是Form2。 我将此代码从vb.net转换为c#,它在窗体应用程序中运行良好,但我遇到了WPF应用程序的问题 - 双**行。我是WPF的初学者。请问有人帮帮我吗?

 public Form1()
    {
        InitializeComponent();
        //Added to support default instance behavour in C#
        if (defaultInstance == null)
            defaultInstance = this;
    }
#region Default Instance
private static Form1 defaultInstance;
 public static Form1 Default
    {
        get
        {
            if (defaultInstance == null)
            {
                defaultInstance = new Form1();
                **defaultInstance.FormClosed += new FormClosedEventHandler(defaultInstance_FormClosed);**
            }

            return defaultInstance;
        }
    }

    static void defaultInstance_FormClosed(object sender, FormClosedEventArgs e)
    {
        defaultInstance = null;
    }
#endregion //Default Instance

1 个答案:

答案 0 :(得分:0)

谢谢大家,我找到了解决方案。如果有人需要它:

行:

defaultInstance.FormClosed += new FormClosedEventHandler(defaultInstance_FormClosed);

替换为:

defaultInstance.Closed += new EventHandler(defaultInstance_WindowClosed);

和行:

static void defaultInstance_FormClosed(object sender, FormClosedEventArgs e)

替换为:

static void defaultInstance_WindowClosed(object sender, EventArgs e)

当然,Form1应该替换为真正的WPF窗口。

就我而言,Form1 =验证。