Blend ---- 旋转按钮
今天在一本书中看到一段关于Blend的使用,所以简单的学习一下,并作一下记录。
一、创建一个Silverlight for Windows Phone工程界面布局大概如下图
View Code
1 <phone:PhoneApplicationPage 2 x:Class="BlendPro.MainPage" 3 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 4 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 5 xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone" 6 xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone" 7 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 8 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 9 mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768" 10 FontFamily="{StaticResource PhoneFontFamilyNormal}" 11 FontSize="{StaticResource PhoneFontSizeNormal}" 12 Foreground="{StaticResource PhoneForegroundBrush}" 13 SupportedOrientations="Portrait" Orientation="Portrait" 14 shell:SystemTray.IsVisible="True"> 15 16 <!--LayoutRoot 是放置所有頁面的根資料格--> 17 <Grid x:Name="LayoutRoot" Background="Transparent"> 18 <Grid.RowDefinitions> 19 <RowDefinition Height="Auto"/> 20 <RowDefinition Height="*"/> 21 </Grid.RowDefinitions> 22 23 <!--TitlePanel 包含應用程式的名稱和頁面標題--> 24 <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28"> 25 <TextBlock x:Name="ApplicationTitle" Text="BlendPro" Style="{StaticResource PhoneTextNormalStyle}"/> 26 <TextBlock x:Name="PageTitle" Text="Blend Pro" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/> 27 </StackPanel> 28 29 <!--ContentPanel - 其他內容置於此--> 30 <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> 31 <Button Content="Search" Margin="0,0,0,100" Height="72" Click="SchButton_Click" Name="SchButton" Width="180"/> 32 </Grid> 33 </Grid> 34 35 </phone:PhoneApplicationPage>
View Code
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Net; 5 using System.Windows; 6 using System.Windows.Controls; 7 using System.Windows.Documents; 8 using System.Windows.Input; 9 using System.Windows.Media; 10 using System.Windows.Media.Animation; 11 using System.Windows.Shapes; 12 using Microsoft.Phone.Controls; 13 14 namespace BlendPro 15 { 16 public partial class MainPage : PhoneApplicationPage 17 { 18 // 建構函式 19 public MainPage() 20 { 21 InitializeComponent(); 22 } 23 24 private void SchButton_Click(object sender, RoutedEventArgs e) 25 { 26 27 } 28 } 29 }
代码很easy
二、在Blend中打开该程序
开启方法 :视图->在Expression Blend中开启
三、添加一个动画故事板(使其在按下按钮时被触发)
在Objects and Timeline 窗口中,单击“+”按钮。这会创建一个新的故事板,并为其取名为“SearchPressAnimation”然后单击OK按钮。你会看见在Objects and Timeline 窗口中出现一条时间线,同时在主窗口中显示了一个记录图标,表示你正在编辑的故事板。(如图)
在0.5秒的过程中,我们把SchButton旋转-180度。为此,大约0.5秒标记处单击并移动黄色竖线,该线标示动画的当前点。确保SchButton控件被选中,在Properties窗口的RenderTransform区域中将旋转角度设置为-180。
接下来在1秒的标记处单击,设置SchButton的角度为-360。按下Play按钮,就可以预览按钮动画了。完成故事板后,单击故事板名称左侧的红色按钮停止编辑故事板。最后关联事件来启动故事板,右击Projects窗口中的Solution节点,选择Edit in Visual Studio返回到VisualStudio,这时回到VisualStudio中要重新载入程序,然后在SchButton的Click事件中启动故事板。
整个代码如下:
View Code
1 <phone:PhoneApplicationPage 2 x:Class="BlendPro.MainPage" 3 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 4 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 5 xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone" 6 xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone" 7 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 8 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 9 mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768" 10 FontFamily="{StaticResource PhoneFontFamilyNormal}" 11 FontSize="{StaticResource PhoneFontSizeNormal}" 12 Foreground="{StaticResource PhoneForegroundBrush}" 13 SupportedOrientations="Portrait" Orientation="Portrait" 14 shell:SystemTray.IsVisible="True"> 15 <phone:PhoneApplicationPage.Resources> 16 <Storyboard x:Name="SearchPressAnimation"> 17 <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.Rotation)" Storyboard.TargetName="SchButton"> 18 <EasingDoubleKeyFrame KeyTime="0" Value="0"/> 19 <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="-180"/> 20 <EasingDoubleKeyFrame KeyTime="0:0:1" Value="-360"/> 21 </DoubleAnimationUsingKeyFrames> 22 </Storyboard> 23 </phone:PhoneApplicationPage.Resources> 24 25 <!--LayoutRoot 是放置所有頁面的根資料格--> 26 <Grid x:Name="LayoutRoot" Background="Transparent"> 27 <Grid.RowDefinitions> 28 <RowDefinition Height="Auto"/> 29 <RowDefinition Height="*"/> 30 </Grid.RowDefinitions> 31 32 <!--TitlePanel 包含應用程式的名稱和頁面標題--> 33 <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28"> 34 <TextBlock x:Name="ApplicationTitle" Text="BlendPro" Style="{StaticResource PhoneTextNormalStyle}"/> 35 <TextBlock x:Name="PageTitle" Text="Blend Pro" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/> 36 </StackPanel> 37 38 <!--ContentPanel - 其他內容置於此--> 39 <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0" RenderTransformOrigin="0.5,0.5"> 40 <Grid.RenderTransform> 41 <CompositeTransform/> 42 </Grid.RenderTransform> 43 <Button Content="Search" Margin="0,0,0,100" Height="72" Click="SchButton_Click" Name="SchButton" Width="180" RenderTransformOrigin="0.5,0.5" > 44 <Button.RenderTransform> 45 <CompositeTransform/> 46 </Button.RenderTransform> 47 </Button> 48 </Grid> 49 </Grid> 50 51 </phone:PhoneApplicationPage>
View Code
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Net; 5 using System.Windows; 6 using System.Windows.Controls; 7 using System.Windows.Documents; 8 using System.Windows.Input; 9 using System.Windows.Media; 10 using System.Windows.Media.Animation; 11 using System.Windows.Shapes; 12 using Microsoft.Phone.Controls; 13 14 namespace BlendPro 15 { 16 public partial class MainPage : PhoneApplicationPage 17 { 18 // 建構函式 19 public MainPage() 20 { 21 InitializeComponent(); 22 } 23 24 private void SchButton_Click(object sender, RoutedEventArgs e) 25 { 26 this.SearchPressAnimation.Begin(); 27 } 28 } 29 }