Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在窗体上显示Usercontrol_C#_User Controls - Fatal编程技术网

C# 在窗体上显示Usercontrol

C# 在窗体上显示Usercontrol,c#,user-controls,C#,User Controls,我正在尝试向窗体显示usercontrol。 usercontrol是一个带有来自鸟的gif图像的picturebox 我正在尝试这样做: //main form class Form1 { //play button private void gameButton1_Click(object sender, EventArgs e) { startPanel.Visible = false; Game g = new Game(this.

我正在尝试向窗体显示usercontrol。 usercontrol是一个带有来自鸟的gif图像的picturebox

我正在尝试这样做:

//main form
class Form1
{
    //play button
    private void gameButton1_Click(object sender, EventArgs e)
    {
        startPanel.Visible = false;
        Game g = new Game(this.gamePanel1);
        g.Start();
    }
}

class Game
{
    //is set to true after starting game

    public Game(gamePanel Display)
    {
        this.Display = Display;
    }

    bool Running = false;

    public gamePanel Display;

    public void Start()
    {

        Thread create_BirdsThread = new Thread(new ThreadStart(create_Birds));
        create_BirdsThread.Start();
        Running = true;

    }

    private void create_Birds()
    {
        while (Running)
        {

            //Display is a variable from class 'GamePanel'
            Display.CreateBirds();
            Display.Refresh();

            //this is just to test that one bird works
            Running = false;
        }
    }
}


class gamePanel : UserControl
{
    public void CreateBirds()
    {
        yBird y = new yBird();
            y.BackColor = System.Drawing.Color.Transparent;
            y.Location = new System.Drawing.Point(32, 56);
            y.Size = new System.Drawing.Size(96, 65);
            y.TabIndex = 1;

            if (this.InvokeRequired)
            {
                this.BeginInvoke((MethodInvoker)delegate()
                {
                    this.Controls.Add(y);
                });
            }
            else
            {
                this.Controls.Add(y);
            }
            y.Visible = true;
            y.BringToFront();
    }
}
但它没有在我的屏幕上显示一只鸟。 如何解决这个问题

谢谢

*编辑:


我从我的主窗体和Game.Start()方法中添加了代码

当然,此代码无法工作。为了使Control.Begin/Invoke()工作,.NET首先需要知道代码需要在哪个特定线程上运行。从Handle属性中可以看出。它告诉它哪个特定线程拥有这个窗口,底层的winapi调用是

但在代码中这是一个鸡和蛋的问题。在代码调用Controls.Add()方法之前,不会创建本机窗口。它必须在程序的UI线程上运行,即拥有表单对象的线程。Windows不允许子窗口由其他线程拥有

所以它不知道调用哪个线程。如果您强制创建句柄,那么由于所有权规则,您的程序将以不同的方式终止

请注意更大的问题:您正在创建一个线程来运行代码,这只需要几纳秒。这些财产转让非常便宜。真正的工作是创建窗口,这需要很多微秒。您希望(并且需要)在UI线程上运行的。因此,使用线程没有任何好处,UI线程也不会花费几纳秒的时间来设置属性


因此,完全移除螺纹以获得领先。如果你正在做一些其他慢的事情,比如加载图像,那么这可以在工作线程上轻松完成。

我已经修复了它

我刚换了这个

if (this.InvokeRequired)
            {
                this.BeginInvoke((MethodInvoker)delegate()
                {
                    this.Controls.Add(y);
                });
            }
对此

if (this.InvokeRequired)
            {
                this.Invoke(new MethodInvoker(this.yellow));

            }

您是否在任何地方调用了
create_Birds()
,如
表单.Load
事件?另外,请在您的问题中添加标记,以明确您编写的是什么样的程序,WinForms、WPF ASP、XNA……?您还可以显示yBird类吗?@MichalKomorowski yBird类只不过是一些使用语句和带有“InitializeComponent();”的构造函数而已非常感谢。但这只是一个测试。我真正想做的是一直创造鸟儿(它们会飞走),直到游戏结束。这就是我需要线的原因。当这起作用时,我想删除
Running=false
,使其继续运行。您可以在工作线程中创建位图,只要确保不会同时访问它们。因此,在一个线程中绘制一个后台缓冲区并在UI线程中显示它的场景可以工作。到目前为止,这些都与你所得到的相去甚远。请结束你的问题。