以编程方式加载的3d对象的动画,故事板不以start开头

时间:2017-02-24 23:01:13

标签: c# wpf animation rotation storyboard

我无法运行animation。我搜索了网络和网站,我做了几个小时的更改,但故事板没有启动。我不会使用BeginAnimation。我希望我的问题也可以让其他人了解Storyboard

enter code here

    using HelixToolkit.Wpf;
    using System;
    using System.Windows;
    using System.Windows.Media.Animation;
    using System.Windows.Media.Media3D;

    namespace test_storyboard_02    
    {
       public partial class MainWindow : Window
       {
          public Storyboard myStoryboard = new Storyboard();

          Model3DGroup cubelet;
          public MainWindow()
          {
              InitializeComponent();
          }

          private void Window_Loaded(object sender, RoutedEventArgs e)
          {
             LoadObj();
             view1.ZoomExtents();

             FrameworkContentElement element = new FrameworkContentElement();
             NameScope.SetNameScope(element, new NameScope());


             AxisAngleRotation3D rotation = new AxisAngleRotation3D(new Vector3D(0, 0, 1), 180);
             RotateTransform3D myRotateTransform3D = new RotateTransform3D(rotation, new Point3D(0, 0, 0));

             cubelet.Transform = myRotateTransform3D;

             element.RegisterName("rotation", rotation);

             DoubleAnimation animation = new DoubleAnimation();
             animation.By = 5;
             animation.Duration = TimeSpan.FromSeconds(0);


             Storyboard.SetTarget(animation, rotation);
             Storyboard.SetTargetProperty(animation, new PropertyPath("Angle"));
             myStoryboard.Children.Add(animation);

             myStoryboard.Duration = TimeSpan.FromSeconds(4);
             myStoryboard.Begin(element, HandoffBehavior.Compose);

         }
         private void LoadObj()
         {
            view1.Children.Clear();
            //cubelets = new Model3DGroup[1, 1, 1];
            cubelet = new Model3DGroup();
            ModelImporter importer = new ModelImporter();
            Model3D ModelCube = importer.Load(@"e:\x.obj");
            cubelet.Children.Add(ModelCube);
            view1.Children.Add(new ModelVisual3D { Content = cubelet });
         }
     }
 }

3 个答案:

答案 0 :(得分:0)

这些代码行存在问题:

FrameworkContentElement element = new FrameworkContentElement();
NameScope.SetNameScope(element, new NameScope());
...
element.RegisterName("rotation", rotation);

变更:

NameScope scope = new NameScope();
FrameworkContentElement element = new FrameworkContentElement();
NameScope.SetNameScope(element, scope);
...
element.RegisterName("rotation", scope);

看到这个解决了你的问题。

答案 1 :(得分:0)

这是我使用的xaml文件。

<Window x:Class="test_storyboard_02.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:test_storyboard_02"
    xmlns:HelixToolkit="clr-namespace:HelixToolkit.Wpf;assembly=HelixToolkit.Wpf"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
<Grid>

    <Canvas Name="LayoutRoot" HorizontalAlignment="Left" Height="319" VerticalAlignment="Top" Width="517">
        <HelixToolkit:HelixViewport3D x:Name="view1" Background="Gray" Height="278" Canvas.Left="25" Canvas.Top="22" Width="463">
            <HelixToolkit:DefaultLights/>
            <!--<local:RubikCube x:Name="cube1" /> -->

        </HelixToolkit:HelixViewport3D>

    </Canvas>
</Grid>

答案 2 :(得分:0)

已解决最后该程序工作,感谢@AnjumSKhan。我希望我附上的代码也可以提供给其他人。还要感谢stackoverflow。

MainWindow.xaml

<Window x:Class="test_storyboard_02.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:test_storyboard_02"
    xmlns:HelixToolkit="clr-namespace:HelixToolkit.Wpf;assembly=HelixToolkit.Wpf"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
<Window.Resources>
</Window.Resources>
<Grid>

    <Canvas Name="LayoutRoot" HorizontalAlignment="Left" Height="319" VerticalAlignment="Top" Width="517">
        <HelixToolkit:HelixViewport3D x:Name="view1" Background="Gray" Height="278" Canvas.Left="25" Canvas.Top="22" Width="463">
            <HelixToolkit:DefaultLights/>
            <!--<local:RubikCube x:Name="cube1" /> -->
        </HelixToolkit:HelixViewport3D>
    </Canvas>
</Grid>

MainWindow.xaml.cs

    using HelixToolkit.Wpf;
using System;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Media3D;
using System.Windows.Shapes;

namespace test_storyboard_02
{
    public partial class MainWindow : Window
    {
        public Storyboard myStoryboard = new Storyboard();
        public Model3DGroup MainModel3Dgroup = new Model3DGroup();

        Model3DGroup modelFloor;
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            LoadObj();
            view1.ZoomExtents();
            NameScope scope = new NameScope();
            FrameworkContentElement element = new FrameworkContentElement();
            NameScope.SetNameScope(element, scope);

            // Create a box  that will be the target
            // of the animation.
            // Material material = HelixToolkit.Wpf.MaterialHelper.CreateMaterial(Colors.DarkBlue);
            // MeshBuilder meshBuilder = new MeshBuilder();
            // meshBuilder.AddBox(new Point3D(0, 0, 0), 200, 200, 200);
            // GeometryModel3D modelFloor = new GeometryModel3D(meshBuilder.ToMesh(), material);
            // modelFloor.SetName("floor");
            // MainModel3Dgroup.Children.Add(modelFloor);

            var lights = new DefaultLights();
            view1.Children.Add(lights);

            ModelVisual3D model_visual = new ModelVisual3D();
            model_visual.Content = modelFloor; 
            view1.Children.Add(model_visual);
            view1.ZoomExtents();

            AxisAngleRotation3D rotation = new AxisAngleRotation3D(new Vector3D(0, 0, 1), 0);
            RotateTransform3D myRotateTransform3D = new RotateTransform3D(rotation, new Point3D(0, 0, 0));

            modelFloor.Transform = myRotateTransform3D;

            element.RegisterName("rotation", rotation);

            // Create two DoubleAnimations and set their properties.
            DoubleAnimation animation = new DoubleAnimation();
            animation.From = 0;
            animation.To = 200;
            animation.Duration = TimeSpan.FromSeconds(2);

            Storyboard.SetTargetProperty(animation, new PropertyPath("Angle"));
            Storyboard.SetTargetName(animation, "rotation");
            myStoryboard.Children.Add(animation);
            myStoryboard.Duration = TimeSpan.FromSeconds(2);

            // Make the Storyboard a resource.
            this.Resources.Add("unique_id1", myStoryboard);
            myStoryboard.Begin(element, HandoffBehavior.Compose);

        }
        private void LoadObj()
        {
            view1.Children.Clear();
            modelFloor = new Model3DGroup();
            ModelImporter importer = new ModelImporter();
            Model3D ModelCube = importer.Load(@"e:\x.obj");
            modelFloor.Children.Add(ModelCube);
            view1.Children.Add(new ModelVisual3D { Content = modelFloor });
        }
    }
}