简体   繁体   中英

Helix 3D Toolkit - object reference is required for the non-static field

I try to load a 3d Model using an open file dialog so with a button click I can select an .obj file and display it :

Model3DGroup groupLoad  = HelixToolkit.Wpf.ModelImporter.Load("C:....");

    public static Model3DGroup Load(string path)
    {
        if (path == null)
        {
            return null;
        }

        Model3DGroup model = null;
        string ext = System.IO.Path.GetExtension(path).ToLower();
        switch (ext)
        {
            case ".3ds":
                {
                    var r = new HelixToolkit.Wpf.StudioReader();
                    model = r.Read(path);
                    break;
                }

            case ".lwo":
                {
                    var r = new HelixToolkit.Wpf.LwoReader();
                    model = r.Read(path);

                    break;
                }

            case ".obj":
                {
                    var r = new HelixToolkit.Wpf.ObjReader();
                    model = r.Read(path);
                    break;
                }

            case ".objz":
                {
                    var r = new HelixToolkit.Wpf.ObjReader();
                    model = r.ReadZ(path);
                    break;
                }

            case ".stl":
                {
                    var r = new HelixToolkit.Wpf.StLReader();
                    model = r.Read(path);
                    break;
                }

            case ".off":
                {
                    var r = new HelixToolkit.Wpf.OffReader();
                    model = r.Read(path);
                    break;
                }

            default:
                throw new InvalidOperationException("File format not supported.");
        }

        return model;
    }

To solve this issue:

An object reference is required for the non-static field, method, or property 'ModelImporter.Load(string,Dispatcher,bool)'

I have added this:

var importer = new HelixToolkit.Wpf.ModelImporter();
var groupLoad = importer.Load("C:....");

Anyone ideas, what could be wrong here?

The problem is that you try to call the Load method of ModelImporter . ModelImporter is a class and if you try to call a method of a class , then you are attempting to run a static method, that is, a method that is class-level. However, Load is instance level.

Let me give you an example:

If you have a Bird class , that represents all Bird objects and you call Bird.migrate() that is a method executed by all Bird objects. However, if you instantiate Bird , you may call fly . In our example migrate is a method the Bird objects do together, while fly is an individual action. A Bird object can fly , but since fly is an instance-level action, it cannot be executed by all the birds.

To remedy this problem, since Load is an instance-level method, you will have to instantiate the class :

ModelImporter import = new ModelImporter();  
device = import.Load(model);

Here you create an individual ModelImporter instance and thus, Load will be accessible through it. If a method is static , then it is class -level, otherwise it is instance-level.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM