2013-10-03 96 views
0

对动画作品的小例子,不起作用的模型性质的变化,在动画WPF动画与MVVM模式

<Window 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="MainWindow" Height="440" Width="732" Loaded="Window_Loaded" 
    MouseLeftButtonDown="Grid_MouseLeftButtonDown" MouseLeftButtonUp="Grid_MouseLeftButtonUp" MouseMove="Grid_MouseMove"> 
<Window.Resources> 

</Window.Resources> 
<Grid>   
    <Grid.RowDefinitions> 
     <RowDefinition Height="20"></RowDefinition> 
     <RowDefinition Height="*"></RowDefinition> 
     <RowDefinition Height="20"></RowDefinition> 
    </Grid.RowDefinitions>   
    <Canvas Name="canvMain" Height="360" Width="710" Grid.Row="1"> 
     <Rectangle Name="Zombi" Width="20" Height="40" Stroke="Black" StrokeThickness="2" Canvas.Bottom="0" Canvas.Right="{Binding Path=X, UpdateSourceTrigger=PropertyChanged, NotifyOnTargetUpdated=True}"></Rectangle> 
    </Canvas> 
</Grid> 

public double X 
    { 
     get { return _x; } 
     set 
     { 
      if (!double.IsNaN(value)) 
      { 
       _x = value; 
       OnPropertyChanged("X"); 
      } 
     } 
    } 

double pixelMetr = 0.715; 

    private int GetPixel(double metr) 
    { 
     return Convert.ToInt32(metr/pixelMetr); 
    } 

private void StartZombi() 
    { 
     double distance = 250; 
     double maxTime = (new Random()).Next(5, 10); 

     PathGeometry animationPath = new PathGeometry(); 

     LineGeometry lineGeometry = new LineGeometry(); 
     lineGeometry.StartPoint = new Point(0, 0); 
     lineGeometry.EndPoint = new Point(GetPixel(distance), 0); 

     animationPath.AddGeometry(lineGeometry); 

     DoubleAnimationUsingPath animationX = new DoubleAnimationUsingPath(); 
     animationX.PathGeometry = animationPath; 
     animationX.Duration = TimeSpan.FromSeconds(maxTime); 
     animationX.Source = PathAnimationSource.X; 
     Storyboard.SetTarget(animationX, Zombi); 
     Storyboard.SetTargetProperty(animationX, new PropertyPath(Canvas.RightProperty)); 

     Storyboard pathAnimationStoryboard = new Storyboard(); 
     pathAnimationStoryboard.RepeatBehavior = RepeatBehavior.Forever; 
     pathAnimationStoryboard.Children.Add(animationX); 

     pathAnimationStoryboard.Begin(this); 
    } 

你好,为什么不工作性质的变化,我当动画运行时,需要获取属性'canvas.right'的当前值。

这是xaml代码。

+0

TestX是什么?如果这是出于显示目的,那么您还需要通知该属性更改。 – Sheridan

+0

TextX - 不要注意,这只是另一个属性 –

+0

请显示XAML的其余部分...我不知道你甚至试图制作动画。 – Sheridan

回答

0

OK,我发现您的解决方案......所有你需要做的是创造一个XDependencyProperty动态显示,而不是:

public static readonly DependencyProperty XProperty = DependencyProperty. 
    Register("X", typeof(double), typeof(MainWindow)); 

public double X 
{ 
    get { return (double)GetValue(XProperty); } 
    set { SetValue(XProperty, value); } 
} 

然后动画方法:

private void StartZombi() 
{ 
    double distance = 250; 
    double maxTime = (new Random()).Next(5, 10); 

    PathGeometry animationPath = new PathGeometry(); 

    LineGeometry lineGeometry = new LineGeometry(); 
    lineGeometry.StartPoint = new Point(0, 0); 
    lineGeometry.EndPoint = new Point(GetPixel(distance), 0); 

    animationPath.AddGeometry(lineGeometry); 

    DoubleAnimationUsingPath animationX = new DoubleAnimationUsingPath(); 
    animationX.PathGeometry = animationPath; 
    animationX.Duration = TimeSpan.FromSeconds(maxTime); 
    animationX.Source = PathAnimationSource.X; 
    Storyboard.SetTarget(animationX, This); // <<< 'This' is the Window.Name 
    Storyboard.SetTargetProperty(animationX, 
     new PropertyPath(MainWindow.XProperty)); 

    Storyboard pathAnimationStoryboard = new Storyboard(); 
    pathAnimationStoryboard.RepeatBehavior = RepeatBehavior.Forever; 
    pathAnimationStoryboard.Children.Add(animationX); 

    pathAnimationStoryboard.Begin(this); 
} 

和XAML:

<Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="Auto"></RowDefinition> 
     <RowDefinition Height="*"></RowDefinition> 
    </Grid.RowDefinitions> 
    <Canvas Name="canvMain" Height="360" Width="710" Grid.Row="1"> 
     <Rectangle Name="Zombi" Width="20" Height="40" Stroke="Black" 
      StrokeThickness="2" Canvas.Bottom="60" Canvas.Right="{Binding Path=X, 
      UpdateSourceTrigger=PropertyChanged}" /> 
    </Canvas> 
    <TextBlock Grid.Row="0" FontSize="26" Text="{Binding X}" /> 
</Grid> 

你需要做的最后一件事是m ake这项工作是将Window.Name财产设置为"This"

要回答你原来的问题,为什么你的代码不工作,我不是100%肯定,但我认为,这是因为Canvas.X属性未插入INotifyPropertyChanged接口等你原来的CLR X财产二传手从未被称为。 Binding与集合的Count属性有类似的情况...当您将项目添加到集合时它不会更新UI,因为它不会通知更改。