はWPF

2011-01-21 3 views
0

にXAMLするには、カスタムクラスの追加だから私はSprite.csこのクラスを作成しました:私は今、何をしたいかはWPF

class Sprite : INotifyPropertyChanged 
{ 
    double _Speed;   
    RectangleGeometry _SpriteRectangleGeometry; 
    Path _SpritePath; 
    public Sprite() 
    { 
     _SpriteRectangleGeometry = new RectangleGeometry(); 
     _SpriteRectangleGeometry.Rect = new Rect(0, 0, 50, 50); 
     Speed = 50; 
     _SpritePath = new Path(); 
     Color = Brushes.Black; 
     _SpritePath.Data = _SpriteRectangleGeometry; 
    } 
    public Sprite(double xpos, double ypos, double height, double width, double speed, SolidColorBrush color) 
    { 
     _SpriteRectangleGeometry = new RectangleGeometry(); 
     _SpriteRectangleGeometry.Rect = new Rect(xpos, ypos, width, height); 
     this.Speed = speed; 
     _SpritePath = new Path(); 
     this.Color = color; 
     _SpritePath.Data = _SpriteRectangleGeometry; 
    } 
    public double XPos 
    { 
     get { return _SpriteRectangleGeometry.Rect.X; } 
     set 
     { 
      _SpriteRectangleGeometry.Rect = new Rect(value, YPos, Width, Height); 
      //Notify the binding that the value has changed. 
      this.OnPropertyChanged("XPos"); 
     } 
    } 
    public double YPos 
    { 
     get { return _SpriteRectangleGeometry.Rect.Y; } 
     set 
     { 
      _SpriteRectangleGeometry.Rect = new Rect(XPos, value, Width, Height); 
      //Notify the binding that the value has changed. 
      this.OnPropertyChanged("YPos"); 
     } 
    } 
    public double Speed 
    { 
     get { return _Speed; } 
     set { _Speed = value; } 
    } 
    public double Width 
    { 
     get { return _SpriteRectangleGeometry.Rect.Width; } 
     set { _SpriteRectangleGeometry.Rect = new Rect(XPos, YPos, value, Height); } 
    } 
    public double Height 
    { 
     get { return _SpriteRectangleGeometry.Rect.Height; } 
     set { _SpriteRectangleGeometry.Rect = new Rect(XPos, YPos, Width, value); } 
    } 
    public SolidColorBrush Color 
    { 
     get { return (SolidColorBrush)_SpritePath.Fill; } 
     set { _SpritePath.Fill = value; } 
    } 
    public event PropertyChangedEventHandler PropertyChanged; 
    protected void OnPropertyChanged(string strPropertyName) 
    { 
     if (PropertyChanged != null) 
      PropertyChanged(this, new PropertyChangedEventArgs(strPropertyName)); 
    } 
} 

は、XAMLにスプライトのインスタンスを追加することですが、私が行うとき、私はこのエラーを取得します:

The value of type 'Sprite" cannot be added to collection or dictionary of type UIElementCollection

アドバイスはありますか?

答えて

5

スプライトは、UIElementCollectionに追加されるUIElementクラスから派生する必要があります。また、ContentControlでラップし、DataTemplateを指定すると、スプライトオブジェクトにはUIElementが作成されます。

1
あなたはリソースセクションだけではなく、インラインに追加する(そしてそれがキーを持っていることを確認)する必要が

<src:Sprite x:Key="data"/>

また、ファイルの先頭にあなたの名前空間を宣言している必要があり