将对象传递给UITypeEditor [英] Passing objects to a UITypeEditor

查看:250
本文介绍了将对象传递给UITypeEditor的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前希望使用PropertyGrid来允许用户编辑某些类,但是我碰壁了,将对象传递到他们使用的UITypeEditor.当用户按下下拉菜单时,我要显示一个已加载纹理的列表框以供选择,如果他们要使用纹理,则应用程序尚未加载,但是他们可以单击按钮以从文件对话框中选择一个.如果我在这里没有任何形式的模拟,那么

I am currently hoping to use a PropertyGrid to allow users to edit some of my classes, however I've hit a wall with passing objects to the UITypeEditor(s) they use. When the user presses the drop down I want to show a listbox of already loaded textures to choose from, if they want to use a texture the application hasn't loaded yet they can click a button to choose one from a file dialog. In case I make no sense here a mock of the form:

.

我的问题:要填充列表框,我需要访问从UITypeEditor管理资源列表的类.

My problem: To fill the listbox I need access to the class that manages the list of resources from the UITypeEditor.

现在,我为自己的班级解决了这个问题,方法是为他们提供有关创建其管理对象的参考.然后,在UITypeEditor中,我使用该引用来访问所需的内容.但是对于我尚未编写的类(例如XNA Texture2D类),我无法做到这一点.

Now I've solved this problem for my own classes by giving them a reference on creation to their managing object. In the UITypeEditor I then use that reference to access what I need. However I can't do this for classes I haven't written, such as the XNA Texture2D class.

以下是我正在使用的类:

Here are what the classes I'm using look like:

class StaticGeometryChunk
{
    // Geometry data to draw with. Contains a reference to its managing 
    // class for use in its UITypeEditor.
    public GeometryData { get; set; }
    ....
}

class Material
{
    // These are XNA classes. I can't just add a reference to its managing 
    // class (I think?).
    public Texture2D Texture1 { get; set; }
    public Texture2D Texture2 { get; set; }
    ....
}

我一直在寻找我的选择,而且似乎是:

I've been looking at my options and they seem to be:

  1. 使管理类保持静态.

我真的不想这样做.由于每个资源的加载方式不同,因此存在多个管理类.在这些类之前,还需要创建一些类并将其传递.

I don't really want to do this. There are several managing classes as each resource is loaded differently. There are also classes that need to be created before these and are passed in.

  1. 使管理类成为单例.

我也不想这么做.似乎是隐藏"问题而不是解决"问题的快速而肮脏的方法.我还可能希望将来有几个管理类的选择,这些管理类会被单身人士淘汰.

I don't really want to do this either. It seems like a quick and dirty way to "hide" the problem instead of "solve" it. I also might want the option of having several managing classes in the future which the singletons eliminate.

  1. 创建一个包装类,其中包含对管理类及其目标的引用(例如XNA Texture2D).

这是我目前正在考虑做的事情.它的操作非常简单快捷,但是有些困扰我,但我不知道该怎么办.

This is currently what I'm thinking of doing. Its would be quite simple and quick to do but something about it nags me but I don't know what.

关于上述方法或其他方法的任何想法都可以将我需要的信息传递给UITypeEditor?

Any thoughts on the above or other methods to pass what I need into the UITypeEditor?

感谢您阅读.

推荐答案

在EditValue方法中,将为您提供一个上下文.使用context.Instance访问保存您的属性的对象.该对象还应该包含一个属性,该属性使您可以访问要显示的内容列表.例如,您可以测试context.Instance是否为ITextureProvider,然后对其进行转换并访问纹理.不确定这对您的设计是否有意义,但请告诉我.

In the EditValue method, you are given a context. Use context.Instance to access the object that holds your property. This object should also contain a property that gives you access to the list of things you want to display. You could test if context.Instance is ITextureProvider for example, then cast it and access the textures. Not sure if this makes sense in your design but let me know.

这篇关于将对象传递给UITypeEditor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆