3

「ゲームエディタ」にPropertyGridを使用して、オブジェクトなどを簡単に編集しようとしましたが、不明なタイプをどのように処理しますか?

たとえば、私はXNAを使用し、Texture2DはXNAフレームワークの一種です(Point / Vectorなどの他のものはうまく機能します)が、Texture2Dはコアのビットマップにすぎないため、不明なタイプを「処理」して、 PropertyGridはそれらを表示できますか?

4

2 に答える 2

4

TypeConvertersを使用できます

TypeConverterを継承し、基本的な動作を提供するジェネリック型コンバーターがあります。最も便利なものの1つは、クラスインスタンスを拡張するために使用できるExpandableObjectConverterです。

    [TypeConverter( typeof( ExpandableObjectConverter ) )]
    public PhysicsObject Physics { get; private set; }

これは私のPoint3構造とそのカスタムタイプコンバーターの例です:

namespace Microsoft.Xna.Framework
{
#if WINDOWS
    public class Point3Converter: System.ComponentModel.ExpandableObjectConverter
    {
        public override bool CanConvertFrom( System.ComponentModel.ITypeDescriptorContext context, Type sourceType )
        {
            return sourceType == typeof( string );
        }

        public override object ConvertFrom( System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value )
        {
            try
            {
                string[] tokens = (( string ) value).Split( ';' );
                return new Point3( int.Parse( tokens[0] ), int.Parse( tokens[1] ), int.Parse( tokens[2] ) );
            }
            catch
            {
                return context.PropertyDescriptor.GetValue( context.Instance );
            }
        }

        public override object ConvertTo( System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType )
        {
            Point3 p = ( Point3 ) value;
            return p.X +";"+ p.Y+";" + p.Z;
        }
    }

    [System.ComponentModel.TypeConverter( typeof( Point3Converter ) )]
#endif
    public struct Point3
    {
        public int X,Y,Z;

        public static readonly Point3 UnitX = new Point3( 1, 0, 0 );
        public static readonly Point3 UnitY = new Point3( 0, 1, 0 );
        public static readonly Point3 UnitZ = new Point3( 0, 0, 1 );

        public Point3( int X, int Y, int Z )
        {
            this.X = X;
            this.Y = Y;
            this.Z = Z;
        }

        public static Vector3 operator +( Point3 A, Vector3 B )
        {
            return new Vector3( A.X + B.X, A.Y + B.Y, A.Z + B.Z );
        }

        public static Point3 operator +( Point3 A, Point3 B )
        {
            return new Point3( A.X + B.X, A.Y + B.Y, A.Z + B.Z );
        }

        public static Point3 operator -( Point3 A, Point3 B )
        {
            return new Point3( A.X - B.X, A.Y - B.Y, A.Z - B.Z );
        }

        public static Point3 operator -( Point3 A )
        {
            return new Point3( -A.X, -A.Y, -A.Z );
        }


        public override string ToString( )
        {
            return X+";"+Y+";"+Z;
        }
    }  
}
于 2012-11-02T16:08:04.030 に答える
0

「カスタム」タイプと「デフォルト」タイプの違いがはっきりしない場合、Texure2DはxnaフレームワークでVector3またはPointとして定義されています。しかし、これらにはDataTypeコンバーターがあります。「カスタム」タイプだと思うタイプを作成する場合は、TypeConverterを使用する必要がありますが、一般的な方法で表示されるプロパティをカスタマイズする場合は、カスタムPropertyTabとカスタムPropertyDescriptorsを使用できます。これが例です。 ozcandegirmenci.com/post/2008/08/…、あなたはそれがよりハードコアな解決策であることに気付くでしょう... – Blau

http://www.ozcandegirmenci.com/post/2008/08/Extending-PropertyGrid-Adding-custom-PropertyTab-and-PropertyDescritors.aspxリンクが変更されました。ここに到達できます。

新しいリンク:http ://www.ozcandegirmenci.com/extending-propertygrid-adding-custom-propertytab-and-propertydescritors/

于 2018-11-22T14:08:11.390 に答える