多窗口问题 - C#WPF [英] Multiple Window Problems - C# WPF

查看:189
本文介绍了多窗口问题 - C#WPF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有使用WPF多个窗口,并使用一个按钮它们之间切换的麻烦。从理论上讲我的应用程序应该有2个按钮,一个前锋各一回分别更改窗口到上一个和下一个窗口。

I am have trouble using multiple windows in WPF and switching between them using a button. In theory my application should have 2 buttons, one forward and one back each on respectively changing the window to the previous and next window.

不幸的是我得到了#1的错误,通过我的研究,我觉得它有什么与我创建再次创建窗口创建一个窗口当新的窗口,从而使一个可怕的循环。但是我不知道在哪里我可以把窗口创建代码,以阻止这个问题,或者是否有其他的方法来解决这个问题。

Unfortunately I get a Stackoverflow error, through my research I feel that it has something to do with me creating new windows that are creating the window again when the previous window is created, thus making a horrible loop. However I don't know where I can put the window creation code to stop this problem or if there are other ways to fix this.

下面是我的Windows代码:

Here is code for my windows:

第一个窗口

public partial class Presenation_1 : Window
{
    Presentation_2 p2 = new Presentation_2();
    MainWindow M = new MainWindow();

    public Presenation_1()
    {
        InitializeComponent();
    }

    private void btnForward_Click(object sender, RoutedEventArgs e)
    {
        this.Close();
        p2.Show();
    }

    private void btnBack_Click(object sender, RoutedEventArgs e)
    {
        this.Close();
        M.Show();
    }
}



第二个窗口

Second Window

public partial class Presentation_2 : Window
{
    Presentation_3 p3 = new Presentation_3();
    Presenation_1 p1 = new Presenation_1();
    public Presentation_2()
    {
        InitializeComponent();
    }

    private void btnForward_Click(object sender, RoutedEventArgs e)
    {
        this.Close();
        p3.Show();
    }

    private void btnBack_Click(object sender, RoutedEventArgs e)
    {
        this.Close();
        p1.Show();
    }
}



第三个窗口

Third Window

public partial class Presentation_3 : Window
{
    Presentation_4 p4 = new Presentation_4();
    Presentation_2 p2 = new Presentation_2();

    public Presentation_3()
    {
        InitializeComponent();
    }

    private void btnForward_Click(object sender, RoutedEventArgs e)
    {
        this.Close();
        p4.Show();
    }

    private void btnBack_Click(object sender, RoutedEventArgs e)
    {
        this.Close();
        p2.Show();
    }
}



第四窗口

Fourth Window

public partial class Presentation_4 : Window
{
    Presentation_3 p3 = new Presentation_3();
    MainWindow M = new MainWindow();

    public Presentation_4()
    {
        InitializeComponent();
    }

    private void btnForward_Click(object sender, RoutedEventArgs e)
    {
        this.Close();
        M.Show();
    }

    private void btnBack_Click(object sender, RoutedEventArgs e)
    {
        this.Close();
        p3.Show();
    }
}



在此先感谢

Thanks in advance

推荐答案

单击该按钮之前,不要创建Windows,你可以在事件处理程序中创建它们:

Don't create your Windows before the button is clicked, you can create them in the event handler:

private void btnForward_Click(object sender, RoutedEventArgs e)
{
    var p2 = new Presentation_2();
    this.Close();
    p2.Show();
}

这篇关于多窗口问题 - C#WPF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆