WPFで複数のウィンドウを使用し、ボタンを使用してそれらを切り替えるのに問題があります。理論的には、私のアプリケーションには2つのボタンがあり、それぞれ1つは前に、もう1つは後ろにあり、それぞれウィンドウを前のウィンドウと次のウィンドウに変更します。
残念ながら、Stackoverflowエラーが発生します。調査の結果、前のウィンドウが作成されたときにウィンドウを再度作成する新しいウィンドウを作成することと関係があり、ひどいループが発生していると感じています。ただし、この問題を防ぐためにウィンドウ作成コードをどこに配置できるか、またはこれを修正する他の方法があるかどうかはわかりません。
これが私のウィンドウのコードです:
最初のウィンドウ
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();
}
}
2番目のウィンドウ
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();
}
}
3番目のウィンドウ
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();
}
}
4番目のウィンドウ
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();
}
}
前もって感謝します