简体   繁体   English

并且非静态字段、方法或属性需要 Object 引用

[英]And Object reference is required for the non-static field, method, or property

I'm attempting to draw things on a form through a class.我试图通过 class 在表格上绘制东西。 Here is the code.这是代码。

public static void DrawStatBars()
    {
        Graphics g = Graphics.FromImage(Graphic.StatBarBackbuffer);
        Font fnt = new Font("Microsoft Sans Serif", 8, FontStyle.Bold);
        g.DrawImage(Graphic.EmptyHPBar, new Point(12, 12));
        g.DrawImage(Graphic.EmptyManaBar, new Point(12, 35));
        g.DrawImage(Graphic.EmptyEXPBar, new Point(12, 58));
        g.DrawImage(Graphic.HPBar, new Rectangle(12, 15, (int)picHpWidth, Graphic.HPBar.Height), new Rectangle(0, 0, (int)picHpWidth, Graphic.HPBar.Height), GraphicsUnit.Pixel);
        g.DrawImage(Graphic.ManaBar, new Rectangle(12, 38, (int)picManaWidth, Graphic.ManaBar.Height), new Rectangle(0, 0, (int)picManaWidth, Graphic.ManaBar.Height), GraphicsUnit.Pixel);
        g.DrawImage(Graphic.EXPBar, new Rectangle(12, 61, (int)picEXPWidth, Graphic.EXPBar.Height), new Rectangle(0, 0, (int)picEXPWidth, Graphic.EXPBar.Height), GraphicsUnit.Pixel);
        g.DrawString(lblHPText, fnt, new SolidBrush(Color.Black), 40, 15);
        g.DrawString(lblManaText, fnt, new SolidBrush(Color.Black), 40, 38);
        g.DrawString(lblEXPText, fnt, new SolidBrush(Color.Black), 40, 63);
        g.Dispose();


        g = frmMainGame.picGeneral.CreateGraphics;
        g.DrawImage(Graphic.StatBarBackbuffer, new Point(0, 0));
        g.Dispose();
    }

The problem is g = frmMainGame.picGeneral.CreateGrpahics;问题是g = frmMainGame.picGeneral.CreateGrpahics; . . Since the control is not static how would I go about accessing it through a class instead of moving the code and having to re-code it to be in the code for the form itself.由于控件不是 static 我将如何 go 关于通过 class 访问它而不是移动代码并且必须将其重新编码为表单本身的代码。

A couple of options spring to mind:几个选项 spring 要记住:

  1. Pass the problem to the caller, ie get them to pass the actual reference in as a parameter.将问题传递给调用者,即让他们将实际引用作为参数传递。 If they don't know, then get their caller to pass it in etc. After all, someone must know which object you need to use.如果他们不知道,请让他们的调用者将其传递等等。毕竟,必须有人知道您需要使用哪个 object。
  2. If you know there's only going to be one reference of interest, then you can store that statically somewhere when the object gets created.如果您知道只有一个感兴趣的引用,那么您可以在创建 object 时将其静态存储在某处。 Depending on the structure of your application you may want to store it in the object's own class, in the creator's class, in the user's class, or in an application class. Depending on the structure of your application you may want to store it in the object's own class, in the creator's class, in the user's class, or in an application class.

You could add an event to handle your PictureBox.Paint method您可以添加一个事件来处理您的 PictureBox.Paint 方法

private void picGeneral_Paint(object sender, PaintEventArgs e)
{
    // Use e.Graphics to do your drawing!
    e.Graphics.DrawStatsBars();
}

Turn your method into an extension method把你的方法变成扩展方法

public static class GraphicsExtensions
{
    public static void DrawStatsBars(this Graphics g)
    {
        // Your code
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 非静态字段,方法或属性需要对象引用 - An object reference is required for the non-static field, method, or property 非静态字段,方法或属性需要对象引用 - An object reference is required for the non-static field, method, or property 非静态字段,方法或属性是否需要对象引用? - An object reference is required for the non-static field, method, or property? 非静态字段,方法或属性需要对象引用 - An object reference is required for the non-static field, method, or property 非静态字段,方法或属性(数据集)需要对象引用 - An object reference is required for the non-static field, method, or property (dataset) 错误:非静态字段,方法或属性需要对象引用 - Error: An object reference is required for the non-static field, method, or property 对象引用是必需的非静态字段,方法或属性错误 - An object reference is required non-static field, method, or property error 非静态字段,方法或属性需要对象引用 - Object reference is required for non-static field, method or property 非静态字段、方法或属性需要 object 引用 - An object reference is required for the non-static field, method, or property 非静态字段方法或属性需要对象引用 - an object reference is required for the non-static field method or property
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM