【问题标题】:Storyboard changing property but not reflected in UI故事板更改属性但未反映在 UI 中
【发布时间】:2010-10-06 07:27:55
【问题描述】:

我在单击按钮时触发了此方法。 MyObj 是一个扩展的 Control 类型,具有两个属性 CenterX 和 CenterY,支持 dp CenterXProperty 和 CenterYProperty。

随着动画播放,MyObj 的 CenterX 和 CenterY 属性发生变化,但我看不到对象的任何移动。

private void MoveMyObjectsWithAnimation(MyObj item, Point point)
    {
        Duration duration = new Duration(TimeSpan.FromSeconds(3));
        Storyboard sb = new Storyboard();
        sb.Duration = duration;

        DoubleAnimation da = new DoubleAnimation();
        da.Duration = duration;
        da.EasingFunction = new SineEase();
        DoubleAnimation da1 = new DoubleAnimation();
        da1.Duration = duration;
        da1.EasingFunction = new SineEase();




        Storyboard.SetTarget(da, item);
        Storyboard.SetTargetProperty(da, new PropertyPath(MyObj.CenterXProperty));

        da.From = item.CenterX;
        da.To = point.X;


        Storyboard.SetTarget(da1, item);
        Storyboard.SetTargetProperty(da1, new PropertyPath(MyObj.CenterYProperty));

        da1.From = item.CenterY;
        da1.To = point.Y;
        sb.Children.Add(da);
        sb.Children.Add(da1);
        sb.Begin();



    }

以下是 MyObj 类中声明的属性系统。

      public double CenterX
    {
        get
        {

         return (double)GetValue(CenterXProperty)+25;
        }
        set
        {
            Canvas.SetLeft(this, value - 25);
            SetValue(CenterXProperty, value);
            OnPropertyChanged(new PropertyChangedEventArgs("CenterX"));
        }
    }
    public double CenterY
    {
        get
        {


            return (double)GetValue(CenterYProperty) + 25;
        }
        set
        {
            Canvas.SetTop(this, value - 25);
            SetValue(CenterYProperty, value);
            OnPropertyChanged(new PropertyChangedEventArgs("CenterY"));

        }
    }

    public static readonly DependencyProperty CenterXProperty =
        DependencyProperty.Register("CenterX", typeof(double), typeof(MyObj), null);

    public static readonly DependencyProperty CenterYProperty =
        DependencyProperty.Register("CenterY", typeof(double), typeof(MyObj), null);

我做错了什么?

【问题讨论】:

  • OnPropertyChanged 在依赖属性上是多余的。 SetValue 在幕后执行此操作。消除那些对初学者的呼吁。

标签: c#-3.0 silverlight-3.0 windows-phone-7 dependency-properties storyboard


【解决方案1】:

这是一个艰难的过程,因为您的代码看起来是正确的。

问题实际上是在为属性设置动画时没有调用 DP 设置器。您的属性设置器从未被命中。

您只能在属性更改时捕获回调。

这行得通:

public double CenterX
{
    get { return (double)GetValue(CentreXProperty); }
    set { SetValue(CentreXProperty, value); }
}

// Using a DependencyProperty as the backing store for CentreX.  
// This enables animation, styling, binding, etc...
public static readonly DependencyProperty CentreXProperty =
    DependencyProperty.Register("CentreX",
                                typeof(double),
                                typeof(MyObj),
                                new PropertyMetadata(0.0,
                                    new PropertyChangedCallback(OnCentreXChanged)));

static void OnCentreXChanged(object sender, DependencyPropertyChangedEventArgs args)
{
    // Get reference to self
    MyObj source = (MyObj)sender;

    // Add Handling Code
    double newValue = (double)args.NewValue;

    Canvas.SetTop(source, newValue - 25);
}

只需为您的 CenterYProperty 复制。

我建议您获取 SLDP sn-p from here 并始终使用它来添加 DP。 (它生成的代码中有一个错字,但 sn-p 很容易修复)。

【讨论】:

  • 感谢 HiTech..这是解决我问题的正确方法..感谢您将我指向 SLDP sn-p。
猜你喜欢
  • 2018-09-20
  • 2020-05-18
  • 2019-10-10
  • 1970-01-01
  • 2012-10-06
  • 1970-01-01
  • 2020-05-02
  • 2017-12-13
  • 1970-01-01
相关资源
最近更新 更多