// X 依賴屬性
public double X {
get { return (double)GetValue(XProperty); }
set { SetValue(XProperty, value); }
}
public static readonly DependencyProperty XProperty =
DependencyProperty.Register("X", typeof(double), typeof(QXSpirit), new PropertyMetadata(0.0));
// Y 依賴屬性
public double Y {
get { return (double)GetValue(YProperty); }
set { SetValue(YProperty, value); }
}
public static readonly DependencyProperty YProperty =
DependencyProperty.Register("Y", typeof(double), typeof(QXSpirit), new PropertyMetadata(0.0));
結構相同,理解 X 即懂 Y。依賴屬性支援綁定、Storyboard 目標屬性等核心機制,傳統 CLR 屬性不具備。
string _ImageAddress;
public string ImageAddress { get { return _ImageAddress; } set { _ImageAddress = value; } }
using WPFGameCourse.Controls;
QXSpirit Spirit = new QXSpirit();
private void InitSpirit() {
Spirit.X = 300;
Spirit.Y = 400;
Spirit.Timer.Interval = TimeSpan.FromMilliseconds(150);
Spirit.ImageAddress = @"..\Player\";
Carrier.Children.Add(Spirit);
}
自由使用 X、Y 依賴屬性,控制幀率(對應施法速度、攻擊速度、凍結/加速 BUFF 等表現層需求)。
地圖移動時,角色地圖座標 (X,Y) 不變,視窗座標 = 地圖座標 + 地圖偏移。有了依賴屬性,跟隨移動極簡單。