Storyboard 學習

private Storyboard PrepareShowStory()
        {
            Storyboard story = new Storyboard();
            DoubleAnimation animation;

            animation = new DoubleAnimation();
            animation.From = 0;
            animation.To = 100;
            animation.Duration = new Duration(TimeSpan.FromMilliseconds(1000));
            Storyboard.SetTarget(animation, image1);
            //Storyboard.SetTargetProperty(animation, new PropertyPath("(UIElement.Opacity)"));
            Storyboard.SetTargetProperty(animation, new PropertyPath("(UIElement.Height)"));
            story.Children.Add(animation);

           

            story.AutoReverse = true;
            story.RepeatBehavior = new RepeatBehavior(3); //RepeatBehavior.Forever;

            return story;
        }

 

 

story.AutoReverse

是否返回,如上面的,先是從0到100,然後再自動由100到0

 

animation.Duration 執行的間隔

 

 

<!--LayoutRoot 是包含所有頁面內容的根網格-->
    <Grid x:Name="LayoutRoot" Background="Transparent">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <!--TitlePanel 包含應用程序的名稱和頁標題-->
        <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
            <TextBlock x:Name="ApplicationTitle" Text="我的應用程序" Style="{StaticResource PhoneTextNormalStyle}"/>
            <TextBlock x:Name="PageTitle" Text="頁面名稱" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
        </StackPanel>
        <Popup x:Name="ProgressPopup" Width="300" IsOpen="False" HorizontalAlignment="Center" 
                   VerticalAlignment
="Top" d:LayoutOverrides="Width, HorizontalMargin" Margin="89,203,91,0">
            <Border BorderThickness="10" BorderBrush="Black" Background="DarkGray" Padding="30,30">
                <StackPanel>
                    <TextBlock MaxHeight="100" Foreground="White"  FontWeight="Bold"  FontSize="36" x:Name="txt" Text="1">
                            <TextBlock.Triggers> 
                                <EventTrigger RoutedEvent="TextBlock.Loaded"> 
                                    <BeginStoryboard> <Storyboard > 
                                        <DoubleAnimation   AutoReverse="True" Duration="0:0:1"
                                     From
="1.0" RepeatBehavior="Forever"  Storyboard.TargetName="txt" Storyboard.TargetProperty="Opacity"   To="0.0"/>  
                                    </Storyboard> 
                                    </BeginStoryboard>                             
                                </EventTrigger>                        
                            </TextBlock.Triggers>
                    </TextBlock>
                </StackPanel>
            </Border>
        </Popup>
        <!--ContentPanel - 在此處放置其他內容-->
        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <Image Height="246" HorizontalAlignment="Left" Margin="101,182,0,0" Name="image1" Stretch="Fill" VerticalAlignment="Top" Width="197" DataContext="{Binding}" Source="/PhoneApp1;component/Images/7.jpg" />
        </Grid>
    </Grid>

public partial class MainPage : PhoneApplicationPage
    {
        Storyboard _timer = new Storyboard();

        Storyboard story;


        int i = 0;
        // 構造函數
        public MainPage()
        {
            InitializeComponent();
            _timer.Duration = TimeSpan.FromMilliseconds(10);
            _timer.Completed += new EventHandler(_timer_Completed);
            _timer.Begin();
            i = Convert.ToInt32(txt.Text);
        }
        void _timer_Completed(object sender, EventArgs e)
        {

            if (i <= txt.MaxHeight)
            {
                i++;
                this.txt.Text = i.ToString();
                _timer.Begin();

                return;
            }

            this.Dispatcher.BeginInvoke(new Action(() => { ProgressPopup.IsOpen = false; }));
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            //ProgressPopup.IsOpen = true;
           
// this.Dispatcher.BeginInvoke(new Action(() => ProgressPopup.IsOpen = true));
            story = PrepareShowStory();
            story.Begin();
        }

        private Storyboard PrepareShowStory()
        {
            Storyboard story = new Storyboard();
            DoubleAnimation animation;

            animation = new DoubleAnimation();
            animation.From = 0;
            animation.To = 100;
            animation.Duration = new Duration(TimeSpan.FromMilliseconds(1000));
            Storyboard.SetTarget(animation, image1);
            //Storyboard.SetTargetProperty(animation, new PropertyPath("(UIElement.Opacity)"));
            Storyboard.SetTargetProperty(animation, new PropertyPath("(UIElement.Height)"));
            story.Children.Add(animation);

            story.AutoReverse = true;
            story.RepeatBehavior = new RepeatBehavior(3); //RepeatBehavior.Forever;

            return story;
        }
    }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章