zoukankan      html  css  js  c++  java
  • wp———图片切换效果

      此篇文章主要是记录一下使用XamlReader加载动画时遇到的一些问题。

    首先呢,把源码附上

     1 <phone:PhoneApplicationPage
     2     x:Class="PicChangedAnimation.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" 
    10     xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"
    11     FontFamily="{StaticResource PhoneFontFamilyNormal}"
    12     FontSize="{StaticResource PhoneFontSizeNormal}"
    13     Foreground="{StaticResource PhoneForegroundBrush}"
    14     SupportedOrientations="Portrait" Orientation="Portrait"
    15     shell:SystemTray.IsVisible="True">
    16     <!--LayoutRoot 是包含所有页面内容的根网格-->
    17     <Grid x:Name="LayoutRoot" Background="Transparent">
    18         <Image x:Name="image" Stretch="Fill" Source="/Image/a.jpg" RenderTransformOrigin="0.5,0.5">
    19             <Image.RenderTransform>
    20                 <CompositeTransform/>
    21             </Image.RenderTransform>
    22             <toolkit:GestureService.GestureListener>
    23                 <toolkit:GestureListener DragCompleted="GestureListener_DragCompleted_1"/>
    24             </toolkit:GestureService.GestureListener>
    25         </Image>
    26         <Image x:Name="image1" Stretch="Fill" Source="/Image/b.jpg" RenderTransformOrigin="0.5,0.5">
    27             <Image.RenderTransform>
    28                 <CompositeTransform/>
    29             </Image.RenderTransform>
    30             <toolkit:GestureService.GestureListener>
    31                 <toolkit:GestureListener DragCompleted="GestureListener_DragCompleted_2"/>
    32             </toolkit:GestureService.GestureListener>
    33         </Image>
    34     </Grid>
    35 
    36 </phone:PhoneApplicationPage>
    View Code
      1 using System;
      2 using System.Windows;
      3 using System.Windows.Markup;
      4 using System.Windows.Controls;
      5 using Microsoft.Phone.Controls;
      6 using System.Windows.Navigation;
      7 using System.Windows.Media.Animation;
      8 
      9 namespace PicChangedAnimation
     10 {
     11     public partial class MainPage : PhoneApplicationPage
     12     {
     13         // 构造函数
     14         public MainPage()
     15         {
     16             InitializeComponent();
     17         }
     18 
     19 
     20         // ====================================================================================================================
     21         private void GestureListener_DragCompleted_1(object sender, DragCompletedGestureEventArgs e) {
     22             if (e.HorizontalChange < -50 && e.HorizontalVelocity < 0) {
     23                 //LeftSlideStoryboard.Begin();
     24                 string moveLeft = -(LayoutRoot.ActualWidth) + "";
     25                 Storyboard sb = InitLeftSlideStoryboard(moveLeft, image1, image);
     26                 if (sb != null) {
     27                     try { 
     28                         sb.Begin(); 
     29                     } catch (Exception ex) { 
     30                         System.Diagnostics.Debug.WriteLine(ex.Message); 
     31                     }
     32                 }
     33                 e.Handled = true;
     34             } else if (e.HorizontalChange > 50 && e.HorizontalVelocity > 0) {
     35                 string moveLeft = LayoutRoot.ActualWidth + "";
     36                 Storyboard sb = InitLeftSlideStoryboard(moveLeft, image1, image);
     37                 if (sb != null) {
     38                     try { 
     39                         sb.Begin(); 
     40                     } catch (Exception ex) { 
     41                         System.Diagnostics.Debug.WriteLine(ex.Message); 
     42                     }
     43                 }
     44                 e.Handled = true;
     45             }
     46         }
     47 
     48         private void GestureListener_DragCompleted_2(object sender, DragCompletedGestureEventArgs e) {
     49             if (e.HorizontalChange < -50 && e.HorizontalVelocity < 0) {
     50                 //LeftSlideStoryboard.Begin();
     51                 string moveLeft = -(LayoutRoot.ActualWidth) + "";
     52                 Storyboard sb = InitLeftSlideStoryboard(moveLeft, image, image1);
     53                 if (sb != null) {
     54                     try { 
     55                         sb.Begin(); 
     56                     } catch (Exception ex) { 
     57                         System.Diagnostics.Debug.WriteLine(ex.Message); 
     58                     }
     59                 }
     60                 e.Handled = true;
     61             } else if (e.HorizontalChange > 50 && e.HorizontalVelocity > 0) {
     62                 string moveLeft = LayoutRoot.ActualWidth + "";
     63                 Storyboard sb = InitLeftSlideStoryboard(moveLeft, image, image1);
     64                 if (sb != null) {
     65                     try { 
     66                         sb.Begin(); 
     67                     } catch (Exception ex) { 
     68                         System.Diagnostics.Debug.WriteLine(ex.Message); 
     69                     }
     70                 }
     71                 e.Handled = true;
     72             }
     73         }
     74 
     75         // ====================================================================================================================
     76 //        // {0}-{-480}
     77 //        private string LEFT_SLIDE_STORYBOARD =
     78 //            @"<Storyboard x:Name=""LeftSlideStoryboard"" 
     79 //                xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
     80 //                xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
     81 //                xmlns:phone=""clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone""
     82 //                xmlns:shell=""clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone""
     83 //                xmlns:d=""http://schemas.microsoft.com/expression/blend/2008""
     84 //                xmlns:mc=""http://schemas.openxmlformats.org/markup-compatibility/2006""
     85 //                mc:Ignorable=""d"" >
     86 //                <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty=""(UIElement.Opacity)"">
     87 //                    <EasingDoubleKeyFrame KeyTime=""0"" Value=""0""/>
     88 //                    <EasingDoubleKeyFrame KeyTime=""0:0:0.6"" Value=""1""/>
     89 //                </DoubleAnimationUsingKeyFrames>
     90 //                <DoubleAnimation Duration=""0:0:0.6"" To=""0"" Storyboard.TargetProperty=""(UIElement.Opacity)"" d:IsOptimized=""True""/>
     91 //                <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty=""(UIElement.RenderTransform).(CompositeTransform.TranslateX)"">
     92 //                    <EasingDoubleKeyFrame KeyTime=""0:0:0.6"" Value=""{0}""/>
     93 //                    <EasingDoubleKeyFrame KeyTime=""0:0:0.61"" Value=""0""/>
     94 //                </DoubleAnimationUsingKeyFrames>
     95 //            </Storyboard>";
     96         private string LEFT_SLIDE_STORYBOARD =
     97             @"<Storyboard x:Name=""LeftSlideStoryboard"" 
     98                 xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
     99                 xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
    100                 xmlns:phone=""clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone""
    101                 xmlns:shell=""clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone""
    102                 xmlns:d=""http://schemas.microsoft.com/expression/blend/2008""
    103                 xmlns:mc=""http://schemas.openxmlformats.org/markup-compatibility/2006""
    104                 mc:Ignorable=""d"" >
    105                 <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty=""(UIElement.Opacity)"">
    106                     <EasingDoubleKeyFrame KeyTime=""0"" Value=""0""/>
    107                     <EasingDoubleKeyFrame KeyTime=""0:0:0.01"" Value=""0""/>
    108                     <EasingDoubleKeyFrame KeyTime=""0:0:0.6"" Value=""1""/>
    109                     <EasingDoubleKeyFrame KeyTime=""0:0:0.61"" Value=""1""/>
    110                 </DoubleAnimationUsingKeyFrames>
    111                 <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty=""(UIElement.Visibility)"">
    112                     <DiscreteObjectKeyFrame KeyTime=""0"">
    113                         <DiscreteObjectKeyFrame.Value>
    114                             <Visibility>Collapsed</Visibility>
    115                         </DiscreteObjectKeyFrame.Value>
    116                     </DiscreteObjectKeyFrame>
    117                     <DiscreteObjectKeyFrame KeyTime=""0:0:0.01"">
    118                         <DiscreteObjectKeyFrame.Value>
    119                             <Visibility>Visible</Visibility>
    120                         </DiscreteObjectKeyFrame.Value>
    121                     </DiscreteObjectKeyFrame>
    122                     <DiscreteObjectKeyFrame KeyTime=""0:0:0.6"">
    123                         <DiscreteObjectKeyFrame.Value>
    124                             <Visibility>Visible</Visibility>
    125                         </DiscreteObjectKeyFrame.Value>
    126                     </DiscreteObjectKeyFrame>
    127                     <DiscreteObjectKeyFrame KeyTime=""0:0:0.61"">
    128                         <DiscreteObjectKeyFrame.Value>
    129                             <Visibility>Visible</Visibility>
    130                         </DiscreteObjectKeyFrame.Value>
    131                     </DiscreteObjectKeyFrame>
    132                 </ObjectAnimationUsingKeyFrames>
    133                 <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty=""(UIElement.Opacity)"">
    134                     <EasingDoubleKeyFrame KeyTime=""0"" Value=""1""/>
    135                     <EasingDoubleKeyFrame KeyTime=""0:0:0.01"" Value=""1""/>
    136                     <EasingDoubleKeyFrame KeyTime=""0:0:0.6"" Value=""0""/>
    137                 </DoubleAnimationUsingKeyFrames>
    138                 <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty=""(UIElement.Visibility)"">
    139                     <DiscreteObjectKeyFrame KeyTime=""0"">
    140                         <DiscreteObjectKeyFrame.Value>
    141                             <Visibility>Visible</Visibility>
    142                         </DiscreteObjectKeyFrame.Value>
    143                     </DiscreteObjectKeyFrame>
    144                     <DiscreteObjectKeyFrame KeyTime=""0:0:0.01"">
    145                         <DiscreteObjectKeyFrame.Value>
    146                             <Visibility>Visible</Visibility>
    147                         </DiscreteObjectKeyFrame.Value>
    148                     </DiscreteObjectKeyFrame>
    149                     <DiscreteObjectKeyFrame KeyTime=""0:0:0.6"">
    150                         <DiscreteObjectKeyFrame.Value>
    151                             <Visibility>Visible</Visibility>
    152                         </DiscreteObjectKeyFrame.Value>
    153                     </DiscreteObjectKeyFrame>
    154                     <DiscreteObjectKeyFrame KeyTime=""0:0:0.61"">
    155                         <DiscreteObjectKeyFrame.Value>
    156                             <Visibility>Collapsed</Visibility>
    157                         </DiscreteObjectKeyFrame.Value>
    158                     </DiscreteObjectKeyFrame>
    159                 </ObjectAnimationUsingKeyFrames>
    160                 <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty=""(UIElement.RenderTransform).(CompositeTransform.TranslateX)"">
    161                     <EasingDoubleKeyFrame KeyTime=""0:0:0.6"" Value=""{0}""/>
    162                     <EasingDoubleKeyFrame KeyTime=""0:0:0.61"" Value=""0""/>
    163                 </DoubleAnimationUsingKeyFrames>
    164             </Storyboard>";
    165         
    166         public Storyboard InitLeftSlideStoryboard(string moveLeft, Image backCtrl, Image frontCtrl) {
    167             string displayXaml = string.Format(LEFT_SLIDE_STORYBOARD, moveLeft);
    168             Storyboard storyboard = null;
    169             try {
    170                 storyboard = XamlReader.Load(displayXaml) as Storyboard;
    171                 Storyboard.SetTarget(storyboard.Children[0], backCtrl);
    172                 Storyboard.SetTarget(storyboard.Children[1], backCtrl);
    173                 Storyboard.SetTarget(storyboard.Children[2], frontCtrl);
    174                 Storyboard.SetTarget(storyboard.Children[3], frontCtrl);
    175                 Storyboard.SetTarget(storyboard.Children[4], frontCtrl);
    176             } catch (Exception e) { 
    177                 System.Diagnostics.Debug.WriteLine(e.Message); 
    178             }
    179             return storyboard;
    180         }
    181 
    182     }
    183 }
    View Code

    然后呢,说下遇到的问题:
    1、“System.Windows.Markup.XamlParseException”类型的第一次机会异常在 System.Windows.ni.dll 中发生undeclared prefix [Line: 1 Position: 42]

      问题出在,加载xaml语句时,存在没有声明就使用的类型前缀

      我们添加上下面的引用就可以了

    1 xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
    2 xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
    3 xmlns:phone=""clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone""
    4 xmlns:shell=""clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone""
    5 xmlns:d=""http://schemas.microsoft.com/expression/blend/2008""
    6 xmlns:mc=""http://schemas.openxmlformats.org/markup-compatibility/2006""
    7 mc:Ignorable=""d"" 

    2、“System.InvalidOperationException”类型的第一次机会异常在 System.Windows.ni.dll 中发生Animation target not specified.
      

      问题出在,没有指定动画的目标控件,但即使我们在xaml中指定了,但一样会出现这样的问题,原因在上面的xaml中没有控件的声明。

      解决方法可以采用c#代码中指定,如下

    1 storyboard = XamlReader.Load(displayXaml) as Storyboard;
    2 Storyboard.SetTarget(storyboard.Children[0], backCtrl);
    3 Storyboard.SetTarget(storyboard.Children[1], backCtrl);
    4 Storyboard.SetTarget(storyboard.Children[2], frontCtrl);
    5 Storyboard.SetTarget(storyboard.Children[3], frontCtrl);
    6 Storyboard.SetTarget(storyboard.Children[4], frontCtrl);

     源码下载

  • 相关阅读:
    [iOS]Xcode+GitHub远程代码托管(GIT, SVN)
    [iOS]Xcode处理过时方法的警告
    [iOS]@synthesize和@dynamic关键字
    [iOS]图片高清度太高, 导致内存过大Crash
    [软件]Xcode查找系统framework所在路径
    [软件]在浏览器里添加MarkDown Here(插件)
    [PHP]利用XAMPP搭建本地服务器, 然后利用iOS客户端上传数据到本地服务器中(四. iOS端代码实现)
    ios -Unity3D的EasyAR集成到已经有项目中。
    iOS创建安全的单例
    阿里云轻量应用服务器 配置mysql详解(转载)
  • 原文地址:https://www.cnblogs.com/qq278360339/p/3210070.html
Copyright © 2011-2022 走看看