【问题标题】:Adding Custom Class to XAML in WPF在 WPF 中向 XAML 添加自定义类
【发布时间】:2011-01-21 16:42:53
【问题描述】:

所以我创建了这个类 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));
    }
}

我现在想要做的是向 Xaml 添加一个 Sprite 实例,但是当我这样做时,我得到了这个错误:

“Sprite”类型的值不能添加到 UIElementCollection 类型的集合或字典中

有什么建议吗?

【问题讨论】:

    标签: c# wpf xaml


    【解决方案1】:

    Sprite 应派生自 UIElement 类以添加到 UIElementCollection。你也可以用ContentControl 包装它并提供一个DataTemplate,它会为你的精灵对象创建一些UIElement

    【讨论】:

      【解决方案2】:

      您必须将它添加到资源部分,而不仅仅是内联(并确保它有一个键)

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

      您还需要在文件顶部声明您的命名空间

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-08-12
        • 2011-03-12
        • 2019-07-29
        • 2013-01-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多