使用terminal.gui时C#无法找到对象引用

时间:2018-11-11 21:59:40

标签: c# .net visual-studio user-interface

我一直在玩一些服务器/客户端的东西,尽管id尝试使用terminal.gui,由于某种原因,我不了解它的作用。我有2个脚本;一个用于处理后端内容,另一个用于处理用户界面/交互。

脚本1:

using UI;

namespace ServerTest
{
    public class Server
    {
        static void Main() 
        {
            UI.UI.main();
            StartServ();
            RecieveConn.Start();
            MngSockets.Start();
        }
    }
}

脚本2:

using ServerTest;
using Terminal.Gui;

namespace UI
{
    public class UI
    {
        static Window win = new Window(new Rect(0, 0, Application.Top.Frame.Width, Application.Top.Frame.Height), "MyApp");
        public static void main()
        {
            Application.Init();
            win.Add(new Label(0, 0, "asd"));

            Application.Run(win);
        }
    }
}

这给了我错误:

System.TypeInitializationException
  HResult=0x80131534
  Message=The type initializer for 'UI.UI' threw an exception.
  Source=Server
  StackTrace:
   at UI.UI.main() in C:\Users\ThisPc\Desktop\ConsoleApp2\ConsoleApp2\ui.cs:line 29
   at ServerTest.Server.Main() in C:\Users\ThisPc\Desktop\ConsoleApp2\ConsoleApp2\Program.cs:line 23

Inner Exception 1:
NullReferenceException: Object reference not set to an instance of an object.

但是,如果我这样做,它会起作用:

using ServerTest;
using Terminal.Gui;

namespace UI
{
    public class UI
    {
        public static void main()
        {
            Window win = new Window(new Rect(0, 0, Application.Top.Frame.Width, Application.Top.Frame.Height), "MyApp");

            Application.Init();
            win.Add(new Label(0, 0, "asd"));

            Application.Run(win);
        }
    }
}

但是我不能在其他功能中使用win变量... 预先感谢=)

1 个答案:

答案 0 :(得分:0)

谢谢你的回答……

只要做

using ServerTest;
using Terminal.Gui;

namespace UI
{
    public class UI
    {
        static Window win = null;
        public static void main()
        {
            win = new Window(new Rect(0, 0, Application.Top.Frame.Width, Application.Top.Frame.Height), "MyApp");

            Application.Init();
            win.Add(new Label(0, 0, "asd"));

            Application.Run(win);
        }
    }
}