WPF中的手动页面加载

时间:2009-07-13 08:42:54

标签: wpf

我的主窗口中有一个按钮,点击此按钮后应重新加载主窗口。怎么做到这一点?

请回复

由于 沙拉斯

1 个答案:

答案 0 :(得分:0)

public partial class Window1 : Window
{
    public Window1()
    {
        InitializeComponent();
    }

    public Button button
    { 
        get
        {
            return this.button1; //Expose the button.
        }
    }
}

public partial class App : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);

        this.MainWindow = new Window1(); //create the window

        this.ShutdownMode = ShutdownMode.OnExplicitShutdown;
        ((Window1)MainWindow).button1.Click += new RoutedEventHandler(button1_Click); //add a handler to that button
        MainWindow.Show();

    }

    void button1_Click(object sender, RoutedEventArgs e)
    {
        this.MainWindow.Close(); //Is disposed
        MainWindow = new Window1(); //recreate
        this.MainWindow.Show(); //reload
    }
}