InitializeComponent()中的C#'System.StackOverflowException'

iyo*_*p45 4 c# stack-overflow

以下是程序的主要部分,其中似乎涉及例外.

using System.Drawing;
using Framework.pages;

namespace Framework
{
   public partial class MainWindow : Window
   {
       public string status;
       public MainWindow()
       {
           InitializeComponent(); // Unhandled exception here
           InitializeTheme();

           activationStep page = new activationStep();
           page.loadPage();
       }
       // etc etc
Run Code Online (Sandbox Code Playgroud)

这是抽象类

 namespace Framework.pages
 {
     abstract class template : MainWindow
     { 
         public abstract void loadPage();
         public abstract void loadTheme();
     }
 }
Run Code Online (Sandbox Code Playgroud)

这是激活步骤类

using System.Windows.Media;

namespace Framework.pages
{
    class activationStep : template
    {
        public override void loadPage()
        {
            //this.loadTheme();
        }

        public override void loadTheme()
        {
            // Default green activation button
            //activateButton.Background = (SolidColorBrush)new BrushConverter().ConvertFromString(Framework.theme.darkGreen);
            //activateButton.BorderBrush = (SolidColorBrush)new BrushConverter().ConvertFromString(Framework.theme.borderGreen);
            // Set form error color to red
            //activationFormError.Foreground = System.Windows.Media.Brushes.Red;
        }
        // etc etc
Run Code Online (Sandbox Code Playgroud)

问题是如果我从MainWindow类中注释掉这两行:

activationStep page = new activationStep();
page.loadPage();
Run Code Online (Sandbox Code Playgroud)

程序运行正常,尽管activStep类中的所有内容都被注释掉了(即使它们没有被注释掉)?我完全不知道为什么我得到这个特殊的例外,因为肯定没有任何强烈的循环或任何东西.

- 值得注意的是,表单中加载的组件确实不多,而且通常运行顺畅.

Gra*_*ICA 7

你正在"创造"一个activationStep,它来源于template,而后者又来自MainWindow,其构造函数创建了一个新的activationStep......等等.

这个循环运行一段时间,然后你得到一个StackOverflowException.

你需要重新考虑你的设计.

  • 问题现在完全解决了. (2认同)