简体   繁体   English

在WPF中将自定义类添加到XAML

[英]Adding Custom Class to XAML in WPF

So I created this class Sprite.cs: 因此,我创建了此类Sprite.cs:

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));
    }
}

What I want to do now is add an instance of Sprite to the Xaml, but when i do i get this error: 我现在想做的是将Sprite实例添加到Xaml,但是当我这样做时,出现此错误:

The value of type 'Sprite" cannot be added to collection or dictionary of type UIElementCollection 无法将“ Sprite”类型的值添加到UIElementCollection类型的集合或字典中

Any advice? 有什么建议吗?

The Sprite should derive from the UIElement class to be added to UIElementCollection . Sprite应该从UIElement类派生,并添加到UIElementCollection Also you could wrap it with ContentControl and provide a DataTemplate which would create some UIElement for your sprite object. 你也可以用ContentControl包装它,并提供一个DataTemplate ,它将为你的精灵对象创建一些UIElement

You have to add it to the resources section rather than just inline (and make sure it has a key) 您必须将其添加到资源部分,而不仅仅是内联(并确保它具有密钥)

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

You also need to have declared your namespace at the top of the file 您还需要在文件顶部声明名称空间

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

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